Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python使用min函数进行分组聚合_Python_Pandas_Aggregate_Pandas Groupby - Fatal编程技术网

python使用min函数进行分组聚合

python使用min函数进行分组聚合,python,pandas,aggregate,pandas-groupby,Python,Pandas,Aggregate,Pandas Groupby,我有一个数据帧,如: df = pd.DataFrame({'year': [2018, 2018, 2018, 2018, 2018, 2018, 2019, 2019, 2019, 2019, 2019, 2019], 'label': ['A', 'B', 'C', 'A', 'B', 'D', 'A', 'E', 'F', 'G', 'E', 'E'], 'cat' : [236, 546, 671, 555,

我有一个数据帧,如:

df = pd.DataFrame({'year': [2018, 2018, 2018, 2018, 2018, 2018, 2019, 2019, 2019, 2019, 2019, 2019],
                   'label': ['A', 'B', 'C', 'A', 'B', 'D', 'A', 'E', 'F', 'G', 'E', 'E'],
                   'cat' : [236, 546, 671, 555, 871, 229, 811, 992, 227, 341, 701, 508],
                   'value': [2, 5, 6, 1, 9, 4, 7, 8, 13, 11, 3, 12]})
我想按
年份
标签
对其进行分组,并为每个分组返回
最低的行。行必须包括(原始)索引值以及所有列(来自
df

因此,结果应该是:

 3  2018  A  555   1
 1  2018  B  546   5
 2  2018  C  671   6
 5  2018  D  229   4
 6  2019  A  811   7
10  2019  E  701   3
 8  2019  F  227  13
 9  2019  G  341  11
到目前为止,我的代码是:

df.groupby(by=['year', 'label']).min().reset_index()
对于
,该值是正确的,但对于
则不正确。(原始)索引也丢失


有什么建议可以解决这个问题吗?

这是一个很好的指示,表明您希望排序然后调用drop\u duplicates,因为
groupby
会破坏原始索引

# This is a little simpler but the order will change.
# df.sort_values('value').drop_duplicates(['year', 'label'])

df.sort_values(['year', 'label', 'value']).drop_duplicates(['year', 'label'])

    year label  cat  value
3   2018     A  555      1
1   2018     B  546      5
2   2018     C  671      6
5   2018     D  229      4
6   2019     A  811      7
10  2019     E  701      3
8   2019     F  227     13
9   2019     G  341     11

这很好地表明您希望排序然后调用drop_duplicates,因为
groupby
会破坏原始索引

# This is a little simpler but the order will change.
# df.sort_values('value').drop_duplicates(['year', 'label'])

df.sort_values(['year', 'label', 'value']).drop_duplicates(['year', 'label'])

    year label  cat  value
3   2018     A  555      1
1   2018     B  546      5
2   2018     C  671      6
5   2018     D  229      4
6   2019     A  811      7
10  2019     E  701      3
8   2019     F  227     13
9   2019     G  341     11

1.首先您需要找到min
res=df.groupby(['Year','label'],as_index=False)['value'].min()


2.然后与原始df合并
final_result=pd.merge(df[['Year','label','cat']],res,on=['Year','label'],how='inner')

1。首先需要找到min
res=df.groupby(['Year','label'],as_index=False)['value'].min()


2.然后与原始df合并
final_result=pd.merge(df[['Year','label','cat']],res,on=['Year','label'],how='inner')

您可以使用
idxmin()
执行以下操作:

输出:

    year label  cat  value
3   2018     A  555      1
1   2018     B  546      5
2   2018     C  671      6
5   2018     D  229      4
6   2019     A  811      7
10  2019     E  701      3
8   2019     F  227     13
9   2019     G  341     11

您可以使用
idxmin()

输出:

    year label  cat  value
3   2018     A  555      1
1   2018     B  546      5
2   2018     C  671      6
5   2018     D  229      4
6   2019     A  811      7
10  2019     E  701      3
8   2019     F  227     13
9   2019     G  341     11