小编Lun*_*ohn的帖子

熊猫 to_datetime() 没有检测到列

我有三列(h1、h2、h3)分别代表日、月和年,例如

import pandas as pd

df = pd.DataFrame({
    'h1': [1,2,3],
    'h2': [1,2,3],
    'h3': [2000,2001,2002]
})
Run Code Online (Sandbox Code Playgroud)

当我执行:

pd.to_datetime(df[['h1', 'h2', 'h3']])
Run Code Online (Sandbox Code Playgroud)

这导致错误:ValueError: to assemble mappings requires at least that [year, month, day] be specified: [day,month,year] is missing 但是当我重命名列然后执行 pd.to_datetime 例如

df=df.rename(columns ={'h1':'day', 'h2':'month', 'h3': 'year'})
df["date_col"] =pd.to_datetime(df[['day','month','year']])
Run Code Online (Sandbox Code Playgroud)

在上面我得到了年份栏,我们必须这样做吗?或者是否可以提供一种格式,以便可以分别将列检测为日、月、年?谢谢。

python pandas

3
推荐指数
1
解决办法
350
查看次数

标签 统计

pandas ×1

python ×1