Reputation: 1202
I am dealing with the following data:
productSold DateTime
Car 2012-05-18 08:09:33
AC 2012-05-23 05:09:33
Table 2012-06-14 04:55:44
I want to group rows with month and create a new dataframe by month.
so, the output should be:
df1:
productSold DateTime
Car 2012-05-18 08:09:33
AC 2012-05-23 05:09:33
df2:
productSold DateTime
Table 2012-06-14 04:55:44
I am at the moment using the following code:
#pf is the original data
pf.index = pd.to_datetime(pf['DateTime'])
a = pf.groupby(by=[pf.index.month, pf.index.year])
Please help me how can I achieve that. Thanks
Upvotes: 1
Views: 22
Reputation: 150735
You can use dt.to_period
to get the month, then groupby
:
monthly = [d for k,d in df.groupby(df.DateTime.dt.to_period('M'))]
print(month[0])
Output:
productSold DateTime
0 Car 2012-05-18 08:09:33
1 AC 2012-05-23 05:09:33
Upvotes: 1