I have a 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.
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
Expected 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
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.