我的代码(来自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)
知道是什么原因引起的吗?
版本信息: