I've managed to group a data frame by Datetime month doing this:
df.set_index('Date').groupby(pd.Grouper(freq='M')).sum()
The data looks like this:
price, debt
2018-4-30, 40.0, 50,0
2018-5-31, 10.0, 0.0
2018-6-30, 30.0, 0.0
2018-7-31, 30.0, 10.0
When I run print(df.index)
, it gives me this:
DatetimeIndex(['2018-04-30', '2018-05-31', '2018-6-30', '2018-7-31'], dtype='datetime64[ns]', name='Date', freq='M')
Is there a way to match a column by the Month/year in the index? So, if I wanted to get a price in July of 2018, it'll return 30.0
. If not, what is the best way to do that? Thank you.