Python programming, with examples in hydraulic engineering and in hydrology.
Showing posts with label groupby. Show all posts
Showing posts with label groupby. Show all posts
Thursday, June 13, 2019
Find Maximum Values by year in timeseries dataframe, keeping the date
df2=df1.ix[df1.groupby(df1.index.year).idxmax().iloc[:,0]]
Thursday, February 8, 2018
Daily precipitation data to monthly and yearly
With pandas, we can group datetime values according to datetime units, as months and years.
If we have a pandas dataframe with 2 columns - DATE and VALUE
Certify that the column DATE is recognized as datetime64 format - you can use, for example:
df1['DATE'] = pd.to_datetime(df1['DATE'])
and then you can group and make operations on this group.
Getting the average of total monthly precipitation:
monthly = df1.groupby(df1['DATE'].dt.month).sum() / len(pd.unique(df1['DATE'].dt.year))
(groups by month sum and then divide by the number of years)
Getting the average yearly precipitation:
PAnual = df1.groupby(df1[0].dt.year).sum().mean()
If we have a pandas dataframe with 2 columns - DATE and VALUE
Certify that the column DATE is recognized as datetime64 format - you can use, for example:
df1['DATE'] = pd.to_datetime(df1['DATE'])
and then you can group and make operations on this group.
Getting the average of total monthly precipitation:
monthly = df1.groupby(df1['DATE'].dt.month).sum() / len(pd.unique(df1['DATE'].dt.year))
(groups by month sum and then divide by the number of years)
Getting the average yearly precipitation:
PAnual = df1.groupby(df1[0].dt.year).sum().mean()
Subscribe to:
Posts (Atom)