Skip to content Skip to sidebar Skip to footer

Pandas Pivot_table On Date

I have a pandas DataFrame with a date column. It is not an index. I want to make a pivot_table on the dataframe using counting aggregate per month for each location. The data look

Solution 1:

I would suggest:

months = cdiff.DATE.map(lambda x: x.month)
pivot_table(cdiff, values='COUNT', rows=[months, 'LOCATION'],
            aggfunc=np.sum)

To get a month name, pass a different function or use the built-in calendar.month_name. To get the data in the format you want, you should call reset_index on the result, or you could also do:

cdiff.groupby([months, 'LOCATION'], as_index=False).sum()


Post a Comment for "Pandas Pivot_table On Date"