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

enter image description here

The first thing I tried was:

df_grouped_vol = pandas_df.groupby(pandas_df['year']).min()[['date', 'vol']]
Asked By: Pirvu Georgian

||

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
Answered By: Timeless
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.