[英]Changing datatime in several columns in dataframe
I'm trying to change the datatime
format of several columns in my dataset however I get: 我试图改变
datatime
在我的数据集数列的格式,但我得到:
ValueError: to assemble mappings requires at least that [year, month, day] be specified: [day,month,year] is missing
I'm not sure why as it works if I run the code for each column individually. 我不确定为什么如果我分别为每一列运行代码,为什么会起作用。
from datetime import datetime
df[['last_pymnt_d',
'next_pymnt_d',
'last_credit_pull_d',
'hardship_start_date',
'hardship_end_date',
'debt_settlement_flag_date',
'settlement_date', 'issue_d',
'earliest_cr_line'
]] = pd.to_datetime(df[['last_pymnt_d',
'next_pymnt_d',
'last_credit_pull_d',
'hardship_start_date',
'hardship_end_date',
'debt_settlement_flag_date',
'settlement_date', 'issue_d',
'earliest_cr_line']], format= '%b-%Y')
You need call function to_datetime
to each column separately, so use DataFrame.apply
: 您需要
to_datetime
对每个列调用函数to_datetime
,因此请使用DataFrame.apply
:
cols= ['last_pymnt_d',
'next_pymnt_d',
'last_credit_pull_d',
'hardship_start_date',
'hardship_end_date',
'debt_settlement_flag_date',
'settlement_date', 'issue_d',
'earliest_cr_line']
df[cols] = df[cols].apply(pd.to_datetime)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.