I have a column with 4000 time data in this format
Time
2021-11-02T09.13.39.156+0000
2021-12-02T09.13.23.679+0000
.....
2022-01-09T09.29.29.696+0000
I can access month, year by this
df['Time'][0].month
df['Time'][0].year
But I want to have three new columns with month-year
, month
, year
.
Can anyone please suggest me how to do this? Thank you.
Use dt
accessor:
df = df.assign(**{'month-year': df['Time'].dt.strftime('%m-%Y'),
'month': df['Time'].dt.month,
'year': df['Time'].dt.year})
print(df)
# Output
Time month-year month year
0 2021-11-02 09:13:39.156000+00:00 11-2021 11 2021
1 2021-12-02 09:13:23.679000+00:00 12-2021 12 2021
2 2022-01-09 09:29:29.696000+00:00 01-2022 1 2022