Pandas aggregation groupby and min
Question:
I have the following data set and I want to return the minimum of vol
grouped by year
but I want also to know on which day (date
column) this minimum occurred. This is a part of a bigger function.
For the example below, the return should be:
1997-07-14 1162876
The first thing I tried was:
df_grouped_vol = pandas_df.groupby(pandas_df['year']).min()[['date', 'vol']]
Answers:
IIUC, use pandas.DataFrame.groupby
with pandas.Series.idxmin
:
g = df.groupby(by="year")
out = df.loc[g["vol"].idxmin(), ["date", "vol"]].squeeze().values
Output :
for e in out:
print("{} {}".format(*e))
#1997-07-14 1162876
I have the following data set and I want to return the minimum of vol
grouped by year
but I want also to know on which day (date
column) this minimum occurred. This is a part of a bigger function.
For the example below, the return should be:
1997-07-14 1162876
The first thing I tried was:
df_grouped_vol = pandas_df.groupby(pandas_df['year']).min()[['date', 'vol']]
IIUC, use pandas.DataFrame.groupby
with pandas.Series.idxmin
:
g = df.groupby(by="year")
out = df.loc[g["vol"].idxmin(), ["date", "vol"]].squeeze().values
Output :
for e in out:
print("{} {}".format(*e))
#1997-07-14 1162876