简体   繁体   中英

Pandas aggregation groupby and min

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM