在pandas dataframe中将日期转换为一年中的一天
python 371
原文标题 :convert date into day of year in pandas data frame
得到一个查询!在给定的数据框中,有一整列包含日期的 DDAY
DDAY MM YYYY
8 1 1975
7 2 1978
14 4 2000
4 5 2013
13 10 2018
将其转换为一年中日期的最快方法是什么?
谢谢!!
回复
我来回复-
jezrael 评论
该回答已被采纳!
只选择要转换为日期时间的列,
rename
它们,所以工作to_datetime
与day, month, year
列和最后添加Series.dt.dayofyear
:d = {'DDAY':'day','MM':'month','YYYY':'year'} df['new'] = pd.to_datetime(df[['DDAY','MM','YYYY']].rename(columns=d)).dt.dayofyear print (df) DDAY MM YYYY new 0 8 1 1975 8 1 7 2 1978 38 2 14 4 2000 105 3 4 5 2013 124 4 13 10 2018 286
2年前