重新格式化Dataframe列到目前为止的格式

时间:2018-02-01 09:13:23

标签: python pandas

我有一个数据框(df),其中包含一列'出生日期'柱:

Date of birth
0       1957-04-30 00:00:00
1       1966-11-10 00:00:00
2       1966-11-10 00:00:00
3       1962-03-28 00:00:00
4       1958-10-28 00:00:00
5       1958-06-04 00:00:00

如何将列重新格式化为仅日期格式?在我重新格式化后,我将从特定日期开始计算年龄:

Date of birth
0       1957-04-30
1       1966-11-10
2       1966-11-10
3       1962-03-28
4       1958-10-28
5       1958-06-04

我尝试过使用

df["Date of birth"] = pd.to_datetime(df['Date of birth'], format='%d%b%Y')
df["Date of birth"] = df["Date of birth"].dt.strftime('%m/%d/%Y')

但没有快乐。

1 个答案:

答案 0 :(得分:1)

在列成为日期后,使用日期访问器来访问它。

df["Date of birth"] = pd.to_datetime(df['Date of birth']).dt.date
相关问题