I have a dataframe that I would like to plot by day and month, year on top of year. In order to do that I understand that I have to put the years into their own columns and index by day and month. I am not sure how to go at it.
Here is a sample data frame
date count
2012-11-12 219
2013-11-12 188
2014-11-12 215
2015-11-12 232
2012-11-13 210
2013-11-13 234
2014-11-13 220
2015-11-13 203
2012-11-14 224
2013-11-14 196
2014-11-14 213
2015-11-14 228
which should look something like this
day-month 2012 2013 2014 2015
11-12 219 188 215 232
11-13 210 234 220 203
11-14 224 196 213 228
Thanks
dates = pd.to_datetime(df['date'])
new_df = df.pivot_table(index=[dates.dt.month, dates.dt.day],
columns=dates.dt.year,
values='count')
new_df = (new_df.set_axis([f'{month}-{day}' for month, day in new_df.index])
.rename_axis(index='month-day', columns=None)
# .reset_index() # if you want column month-day
)
print(new_df)
Output
2012 2013 2014 2015
month-day
11-12 219 188 215 232
11-13 210 234 220 203
11-14 224 196 213 228
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.