I have a dataframe in which i have four columns id,opposition,innings and wickets. I want to group by innings and opposition and want the sum of wicket and count of opposition.
consider this is my dataframe.
and my required output of the dataframe should be
The wickets column is the sum of wickets group by innings and opposition, and the match_play is the count of opposition group by opposition and innings.
I have tried with pivot table but got 'Opposition' not 1-dimensional
table = inn.pivot_table(values=['Opposition', 'Wickets'], index=['Opposition', 'Inning_no'],
aggfunc=['count','sum'])
Just use .groupby()
on a dataframe. And reset_index()
to convert Opposition
and Innings
to normal columns again (they are converted to multiindex during groupby
)
import pandas as pd
df = pd.DataFrame({'id':[1,2,3,4,5], 'Opposition':['Sri Lanka', 'Sri Lanka', 'UAE','UAE','Sri Lanka'],
'Innings':[1,2,1,2,1], 'Wickets':[13,17,14,18,29]})
t = df.groupby(['Opposition', 'Innings'])['Wickets'].agg(Wickets=('sum'),
Match_play=('count')).reset_index()
print(t)
Output:
Opposition Innings Wickets Match_play
0 Sri Lanka 1 42 2
1 Sri Lanka 2 17 1
2 UAE 1 14 1
3 UAE 2 18 1
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.