小编Mat*_*ist的帖子

Python Pandas Dataframe赋值

我正在遵循Lynda教程,他们使用以下代码:

import pandas as pd
import seaborn
flights = seaborn.load_dataset('flights')
flights_indexed = flights.set_index(['year','month'])
flights_unstacked = flights_indexed.unstack()
flights_unstacked['passengers','total']  = flights_unstacked.sum(axis=1)
Run Code Online (Sandbox Code Playgroud)

它完美无缺.但是,在我的情况下,似乎代码没有编译,对于最后一行我一直收到错误.

TypeError:无法将项插入到尚未作为现有类别的CategoricalIndex中

我在视频中知道他们使用的是Python 2,但是因为我正在学习工作(使用Python 3),所以我有Python 3.我能够弄清楚的大多数差异,但是我无法弄清楚如何'total'用乘客的总和来创建这个新的列.

python dataframe python-3.x pandas

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

标签 统计

dataframe ×1

pandas ×1

python ×1

python-3.x ×1