简体   繁体   English

为什么pd.crosstab不起作用?

[英]why pd.crosstab does not work?

The data is from the book "Python for Data Analysis", chp 8, Bar Plots 数据来自《 Python for Data Analysis》(第8章,条形图)一书

tips = pd.read_csv('ch8/tips.csv')
party_counts = pd.crosstab(tips.day,tips.size)

when I run the above codes, I find I can not get the result as the book shows. 当我运行上述代码时,我发现书中没有显示结果。

In [70]: party_counts
Out[70]:
size 1 2 3 4 5 6
day
Fri 1 16 1 1 0 0
Sat 2 53 18 13 1 0
Sun 0 39 15 18 3 1
Thur 1 48 4 5 1 3

my result is 我的结果是

In[36]: party_counts
Out[36]: 
col_0  1708
day        
Fri      19
Sat      87
Sun      76
Thur     62

I test tips' type 我测试提示的类型

In[49]: tips.dtypes
Out[49]: 
total_bill    float64
tip           float64
sex            object
smoker         object
day            object
time           object
size            int64
dtype: object

while I found this question which also has one column is int can get the crosstab result. 虽然我发现这个问题也只有一列是int可以获取交叉表结果。

so, anything wrong with me? 所以,我有什么问题吗? ps, my pandas version is '0.20.2', python 3.6 ps,我的熊猫版本是'0.20.2',python 3.6

size是数据框的一个属性,用于从中获取元素的数量,如果您有一个size列,则需要使用['size']以避免混淆:

pd.crosstab(tips.day, tips['size'])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM