pandas 数据框的简单数据透视表 [英] simple pivot table of pandas dataframe
问题描述
我正在尝试做一个看似非常简单的任务。
给定一个数据帧:
I'm trying to do a seemingly very simple task. Given a dataframe:
daf = pd.DataFrame({'co':['g','r','b', 'r','g','r','b','g'],'sh':['c','s','r','r','r','s, 'c','r']})
daf = pd.DataFrame({'co':['g','r','b','r','g','r','b','g'], 'sh':['c','s','r','r','r','s','c','r']})
co sh
0 g c
1 r s
2 b r
3 r r
4 g r
5 r s
6 b c
7 g r
我想用 co和 sh值的唯一组合计算记录的数量,并输出为带有行['g','r的表','b']和列['c','s','r']
I'd like to count the number of records with the unique combination of 'co' and 'sh' values and output as a table with rows ['g','r','b'] and columns ['c','s','r']
c s r
g 1 0 2
r 0 1 1
b 1 0 1
可以使用pivot_table完成吗?
Can it be done using pivot_table?
谢谢
推荐答案
它可以使用 pandas.crosstab
更简单地完成:
It can be done more simply using pandas.crosstab
:
>>> pandas.crosstab(d.co, d.sh)
sh c r s
co
b 1 1 0
g 1 2 0
r 0 1 2
您可以使用 pivot_table
来完成此操作,但它会给您NaN 0表示缺少连击。您需要指定 len
作为聚合函数:
You can do it with pivot_table
, but it will give you NaN instead of 0 for missing combos. You need to specify len
as the aggregating function:
>>> d.pivot_table(index='co', columns='sh', aggfunc=len)
sh c r s
co
b 1 1 NaN
g 1 2 NaN
r NaN 1 2
这篇关于 pandas 数据框的简单数据透视表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!