小编Cir*_*oth的帖子

类型错误:无法将项目插入还不是现有类别的 CategoricalIndex

我正在尝试在 Pandas 数据框中创建一个新列,表示每行的总和(在这种情况下,该数字表示导入时库随附的 Seaborn Flights 数据集中特定年份的乘客数量。这里是我的代码:

import pandas as pd
import seaborn

flights = seaborn.load_dataset('flights')

flights_indexed = flights.set_index(['year', 'month'])

# create a flights_unstacked DataFrame

flights_unstacked = flights_indexed['passengers'].unstack()

flights_unstacked['total'] = flights_unstacked.sum(axis=1)
Run Code Online (Sandbox Code Playgroud)

我收到几个关键错误:

KeyError: 'total' 在处理上述异常的过程中,发生了另一个异常:...

我相信“关键”错误(双关语)是我也收到的类型错误:

类型错误:无法将项目插入还不是现有类别的 CategoricalIndex

python pandas seaborn

5
推荐指数
1
解决办法
2778
查看次数

标签 统计

pandas ×1

python ×1

seaborn ×1