Sam Coleman
Sam Coleman

Reputation: 59

Average in a dataframe

    Date    Timestamp   μmoles
0   2019-06-11  17:21:35    13.5
1   2019-06-11  17:22:35    13.1
2   2019-06-11  17:23:35    13.0
3   2019-06-11  17:24:35    11.8
4   2019-06-11  17:25:35    11.8
... ... ... ...
394 2019-06-11  23:55:38    0.0
395 2019-06-11  23:56:38    0.0
396 2019-06-11  23:57:38    0.0
397 2019-06-11  23:58:38    0.0
398 2019-06-11  23:59:38    0.0

I'm trying to write a code that calculates the average number of µmoles at 5pm. Any suggestions?

Upvotes: 0

Views: 74

Answers (4)

Samsani Hymavathi
Samsani Hymavathi

Reputation: 134

Here, I'm taking rows which starts with 17: and calculating mean for selected rows only

df[df['Timestamp'].str.startswith('17:')]['μmoles'].astype('float64').mean()

Upvotes: 0

Raghav Sharma
Raghav Sharma

Reputation: 195

You can also try this:

df.loc[df['Timestamp'].str.split(':').str[0] == '17', 'μmoles'].mean()

Upvotes: 0

Kurt Kline
Kurt Kline

Reputation: 2079

This should do what you are hoping for.

Combine Date and Timestamp fields into a date_time column

df["date_time"] = pd.to_datetime(df['Date'] + ' ' + df['Timestamp'])

Extract the hour into a separate column

df["hour"] = df["date_time"].apply(lambda x: x.hour)

Print output

print(df.groupby(by=["hour"])["μmoles"].mean())

Upvotes: 0

Edwin Cheong
Edwin Cheong

Reputation: 989

Hi here's an example you can try:

df.loc[df['Timestamp'].dt.hour == 17]['μmoles'].mean() # Follows the 24 hour time format

Upvotes: 1

Related Questions