相关疑难解决方法(0)

使用带有margin = True的Pandas pivot_table时出错

我的代码(来自Python Data Science Handbook(O'Reilly)一书):

完全披露:在撰写本文时,该书仍处于早期发布阶段,这意味着它仍然未经编辑且处于原始状态.

import numpy as np
import pandas as pd
import seaborn as sns
titanic = sns.load_dataset('titanic')

titanic.pivot_table('survived', index='sex', columns='class')
Run Code Online (Sandbox Code Playgroud)

结果是:

数据帧

但是,如果我现在尝试使用margins关键字添加总计,则会发生以下错误:

titanic.pivot_table('survived', index='sex', columns='class', margins=True)

TypeError: cannot insert an item into a CategoricalIndex that is not already an existing category
Run Code Online (Sandbox Code Playgroud)

知道是什么原因引起的吗?

版本信息:

  • Python 3.4.2
  • 大熊猫0.16.2
  • numpy 1.9.2

python numpy python-3.x pandas

2
推荐指数
1
解决办法
1501
查看次数

标签 统计

numpy ×1

pandas ×1

python ×1

python-3.x ×1