简体   繁体   English

仅当列中存在某些条件值时才进行分组

[英]Groupby only when certain conditional value exists in a column

I have a df我有一个df

Key1  Key2   Condition  Score
K11   K21     100        1000
K11   K21     200        3000
K11   K21     100        2000
K12   K22     100        12
K12   K22     200        133
K12   K22     100        300
K14   K24     100        144
K14   K24     200        122
K14   K24     100        4000

I want to do groupby for keys columns Key1 and Key2 only when Condition is 100 and find the max value of Score.我只想在 Condition 为 100 时对键列Key1Key2进行groupby并找到 Score 的最大值。

 df_trial=(df['Condition']=='100').groupby(['Key1','Key2'], as_index=False).max('Score')

This code doesnt seem to work and gives error.此代码似乎不起作用并给出错误。 How can I achieve the desired output如何实现所需的 output

Expected output预期 output

Key1  Key2   Condition  max_Score
K11   K21     100        2000
K12   K22     100        300
K14   K24     100        4000

This should work这应该工作

import numpy as np
import pandas as pd


df = pd.DataFrame({'Key1': ['K11', 'K11', 'K11', 'K12', 'K12', 'K12', 'K14', 'K14', 'K14'],
          'Key2': ['K21', 'K21', 'K21', 'K22', 'K22', 'K22', 'K24', 'K24', 'K24'],
          'Condition': [100, 200, 100, 100, 200, 100, 100, 200, 100],
          'Score': [1000, 3000, 2000, 12, 133, 300, 144, 122, 4000]})

cond = df['Condition']==100
df_out = df.loc[cond].groupby(["Key1", "Key2"])['Score'].max().reset_index()
print(df_out)



   Key1 Key2  Score
0  K11  K21   2000
1  K12  K22    300
2  K14  K24   4000

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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