I have the following dataframe a list of dates
debt
date
2017-11-17 16:00:00 77
2017-11-17 17:00:00 73
2017-11-17 18:00:00 74
2017-11-17 19:00:00 73
2017-11-17 20:00:00 74
2017-11-17 21:00:00 71
I am trying to group by the dates to find the average by dates without time
2017-11-17 74
I tried to do this df = df.groupby(df['date'].map(lambda x: x.day)) KeyError: 'date'
Is there any other way to do it please?
If need date
s in index
use DatetimeIndex.date
:
df = df.groupby(df.index.date).mean()
print (df)
debt
2017-11-17 73.666667
print (df.index)
Index([2017-11-17], dtype='object')
But better is for DatetimeIndex
use DatetimeIndex.floor
:
df = df.groupby(df.index.floor('d')).mean()
print (df)
debt
date
2017-11-17 73.666667
print (df.index)
DatetimeIndex(['2017-11-17'], dtype='datetime64[ns]', name='date', freq=None)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With