I want to calculate the average temperature per day within an specific time frame, for example average temperature on the date = 2021-03-11 between time = 14:23:00 and 16:53:00. I tried with a pivot_table the following:
in_range_df = pd.pivot_table(df, values=['Air_temp'],
index=['Date','Time'],
aggfunc={'Air_temp':np.mean})
The dataframe looks like this:
But how can I specify the time range now? Any help is appreciated.
CodePudding user response:
Usually you can use between
:
df[df["Datum"].between('2021-03-11 14:23:00',
'2021-03-11 16:53:00',)]["Air_temp"].mean()
CodePudding user response:
First you should convert Datum
column to datetime
type:
df['Datum'] = pd.to_datetime(df['Datum'])
Then you can use df.loc
like this:
t1 = '2021-03-11 14:23:00'
t2 = '2021-03-11 16:53:00'
df.loc[(df['Datum'] > t1) & (df['Datum'] < t2)]['Air_tmp'].mean()