[英]Running a groupby on a pivot table with Pandas
I have a pivot table that looks like this: 我有一个数据透视表,如下所示:
In [41]: counts
Out[41]:
SourceColumnID 3029903181 3029903182 3029903183 3029903184 ResponseCount
ColID QuestionID RowID
3029903193 316923119 3029903189 773 788 778 803 3142
3029903194 316923119 3029903189 766 799 782 773 3120
[2 rows x 5 columns]
and I'm trying to figure out how I can groupby RowID so that I can get total counts for each column for each RowID (in this case it would just sum up all of them since the 2 are in the same rowid). 并且我试图弄清楚如何对RowID进行分组,以便获得每个RowID的每一列的总计数(在这种情况下,由于2在同一个rowid中,因此将它们全部相加)。
This is the pivot tables index: 这是数据透视表索引:
In [42]: counts.index
Out[42]:
MultiIndex(levels=[[3029903193, 3029903194], [316923119], [3029903189]],
labels=[[0, 1], [0, 0], [0, 0]],
names=[u'ColID', u'QuestionID', u'RowID'])
You'll want to groupby 'RowID'
. 您需要对
'RowID'
进行分组。 Since it's a level on the MultiIndex you pass 'RowID'
to the level
keyword. 由于它是MultiIndex上的级别,因此您可以将
'RowID'
传递给level
关键字。
In [5]: df.groupby(level='RowID').sum()
Out[5]:
3029903181 3029903182 3029903183 3029903184 ResponseCount
RowID
3029903189 1539 1587 1560 1576 6262
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.