Skip to content Skip to sidebar Skip to footer

Group Index By Minute And Compute Average

So I have a pandas dataframe called 'df' and I want to remove the seconds and just have the index in YYYY-MM-DD HH:MM format. But also the minutes are then grouped and the average

Solution 1:

You need first convert index to DatetimeIndex:

df.index=pd.DatetimeIndex(df.index)#another solution#df.index = pd.to_datetime(df.index)print(df['value'].resample('1Min').mean())#another same solution#print (df.resample('1Min')['value'].mean())2015-05-03 00:00:00    60.62015-05-03 00:01:00    60.62015-05-03 00:02:00    60.22015-05-03 00:03:00    59.2Freq:T,Name:value,dtype:float64

Another solution with seting values of seconds in index to 0 by astype:

print(df.groupby([df.index.values.astype('<M8[m]')])['value'].mean())2015-05-03 00:00:00    60.62015-05-03 00:01:00    60.62015-05-03 00:02:00    60.22015-05-03 00:03:00    59.2Name:value,dtype:float64

Post a Comment for "Group Index By Minute And Compute Average"