Home > OS >  convert date into day of year in pandas data frame
convert date into day of year in pandas data frame

Time:03-14

Got a query! In the given data frame, there is entire column DDAY containing date

   DDAY MM  YYYY
    8   1   1975
    7   2   1978
    14  4   2000
    4   5   2013
    13  10  2018


What is the fastest approach to convert it into day of year?

Thanks!!

CodePudding user response:

Select only columns for convert to datetimes, rename them, so working to_datetime with day, month, year columns and last add 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
  • Related