Leonardo Ferreira Leonardo Ferreira - 1 year ago 61
Python Question

Mean of selected values on dataframe

Using:

df = pd.read_csv('values_per_hour.csv')


The dataframe is something like this:

Hour Vel
0 10
1 11
2 15
. .
. .
. .
23 7
0 17
1 3
2 4
. .
. .
. .
23 2
0
... ...


This dataframe has 10 years of data in hours. As can be seeable the first col is hours of days and the second col is velocity of something. The main objective is to take a mean of all velocities when the hour is 0. I just don't realize how to do that, would be helpful if someone could answer.

Answer Source

Boolean indexing with mean:

df.loc[df['Hour'] == 0,'Vel'].mean()

or use query:

df.query('Hour == 0')['Vel'].mean()

If you would like to do every hour use:

df.groupby('Hour')['Vel'].mean()

Or try and make sure column is datetime dtype first:

df['Ano_Mes_Dia_Hora'] = pd.to_datetime(df.Ano_Mes_Dia_Hora) 

df.groupby(df['Ano_Mes_Dia_Hora'].dt.hour)['Vel'].mean()
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download