【发布时间】:2020-06-21 00:53:35
【问题描述】:
我正在尝试在 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)
我收到了一些关键错误:
KeyError: 'total' 在处理上述异常的过程中,又发生了一个异常:...
我相信“关键”错误(双关语)是我也收到的类型错误:
TypeError: 无法将项目插入到尚未存在的类别的 CategoricalIndex 中
【问题讨论】: