我在pandas数据框中的数据如下:
df1 = pd.DataFrame({'A':['yes','yes','yes','yes','no','no','yes','yes','yes','no'],
'B':['yes','no','no','no','yes','yes','no','yes','yes','no']})
Run Code Online (Sandbox Code Playgroud)
所以,我的数据看起来像这样
----------------------------
index A B
0 yes yes
1 yes no
2 yes no
3 yes no
4 no yes
5 no yes
6 yes no
7 yes yes
8 yes yes
9 no no
-----------------------------
Run Code Online (Sandbox Code Playgroud)
我想将其转换为另一个数据框架.预期的输出可以在以下python脚本中显示:
output = pd.DataFrame({'A':['no','no','yes','yes'],'B':['no','yes','no','yes'],'count':[1,2,4,3]})
Run Code Online (Sandbox Code Playgroud)
所以,我的预期输出看起来像这样
--------------------------------------------
index A B count
--------------------------------------------
0 no no 1
1 no yes 2
2 yes no 4
3 yes yes 3
--------------------------------------------
Run Code Online (Sandbox Code Playgroud)
实际上,我可以通过使用以下命令找到所有组合并计算它们: mytable = df1.groupby(['A','B']).size()
然而,事实证明这种组合在一列中.我想将组合中的每个值分成不同的列,并为计数结果再添加一列.有可能吗?我可以提出你的建议吗?先感谢您.