[英]how to use groupby or pivot_table in pandas
I have a dataframe in which i have four columns id,opposition,innings and wickets.我有一个 dataframe,其中我有四列 id、反对、局和三柱门。 I want to group by innings and opposition and want the sum of wicket and count of opposition.
我想按局数和反对数分组,并想要三柱门和反对数的总和。
consider this is my dataframe.考虑这是我的 dataframe。
and my required output of the dataframe should be我需要的 dataframe 的 output 应该是
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. wickets 列是 wickets 组的局数和反对数之和,match_play 是反对组的反对和局数。
I have tried with pivot table but got 'Opposition' not 1-dimensional
我尝试过使用 pivot 表,但得到
'Opposition' not 1-dimensional
table = inn.pivot_table(values=['Opposition', 'Wickets'], index=['Opposition', 'Inning_no'],
aggfunc=['count','sum'])
Just use .groupby()
on a dataframe.只需在 dataframe 上使用
.groupby()
。 And reset_index()
to convert Opposition
and Innings
to normal columns again (they are converted to multiindex during groupby
)并且
reset_index()
再次将Opposition
和Innings
转换为普通列(它们在groupby
期间转换为 multiindex )
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: 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.