![](/img/trans.png)
[英]Groupby count only when a certain value is present in one of the column in pandas
[英]Groupby only when certain conditional value exists in a column
我有一个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
我只想在 Condition 为 100 时对键列Key1
和Key2
进行groupby并找到 Score 的最大值。
df_trial=(df['Condition']=='100').groupby(['Key1','Key2'], as_index=False).max('Score')
此代码似乎不起作用并给出错误。 如何实现所需的 output
预期 output
Key1 Key2 Condition max_Score
K11 K21 100 2000
K12 K22 100 300
K14 K24 100 4000
这应该工作
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.