Home > Back-end >  Pandas aggregation groupby and min
Pandas aggregation groupby and min

Time:01-23

I have the following data set and I want to return the minimum of vol grouped by year but I want also to know in which day (date column) was this minimum occured. 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']]

enter image description here

CodePudding user response:

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
  • Related