[英]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.我有以下数据集,我想返回按year
分组的vol
的最小值,但我还想知道这个最小值发生在哪一天( date
列)。 This is a part of a bigger function.这是更大的 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
: IIUC,使用pandas.DataFrame.groupby
和pandas.Series.idxmin
:
g = df.groupby(by="year")
out = df.loc[g["vol"].idxmin(), ["date", "vol"]].squeeze().values
Output: Output:
for e in out:
print("{} {}".format(*e))
#1997-07-14 1162876
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.