I have written code to calculate the count of feature3 groupby feature1 and feature2
pd.pivot_table(data=train, index=['feature1', 'feature2'], values=['feature3'], aggfunc='count')
Who's output is:
feature1 feature2 feature3
129001 0 4
1 10
2 11
3 22
4 26
5 38
129002 0 6
2 45
5 25
Now I want to calculate max of feature3 groupby feature1
feature1 feature3
129001 38
129002 45
IIUC:
You need the following statement:
df.groupby(level=0)['feature3'].max()
Start with the results of your pivot_table
print(df)
feature3
feature1 feature2
129001 0 4
1 10
2 11
3 22
4 26
5 38
129002 0 6
2 45
5 25
groupby with level 0 of your index and max:
df.groupby(level=0)['feature3'].max()
Output:
feature1
129001 38
129002 45
Name: feature3, dtype: int64
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With