[英]Raise TypeError when adding new column to a pandas DataFrame
I've been watching an online course about data analysis using Python. 我一直在观看有关使用Python进行数据分析的在线课程。 I came across a problem when following exactly what the instructor did.
当我完全按照指导老师的做事时遇到一个问题。 Basically, I pulled a data frame called "flights" from seaborn and set the index "year" and "month" and unstacked it.
基本上,我从seaborn提取了一个名为“航班”的数据框,并设置了索引“ year”和“ month”并进行了堆叠。 The following codes are used:
使用以下代码:
import seaborn
import pandas as pd
flights = seaborn.load_dataset("flights")
flights_indexed = flights.set_index(["year","month"])
flights_unstacked = flights_indexed.unstack()
flights_unstacked
the final data frame is like this 最终的数据帧是这样的
Then I am trying to add a new column called "Total" at the end for the sum of each year using the following code: 然后,我尝试使用以下代码在每年的末尾添加一个名为“总计”的新列:
flights_unstacked["passengers"]["Total"] = flights_unstacked.sum(axis = 1)
But it raised a TypeError: cannot insert an item into a CategoricalIndex that is not already an existing category.
但是它引发了
TypeError: cannot insert an item into a CategoricalIndex that is not already an existing category.
I am new to data manipulation using pandas. 我是使用熊猫进行数据处理的新手。 Anyone can tell me how to fix this?
有人可以告诉我如何解决此问题吗? Is this a version issue, because the online instructor did exactly the same thing but his works just fine.
这是一个版本问题,因为在线讲师做了完全一样的事情,但是他的工作还不错。 PS: I use Python 2.7 and pandas 0.20.3.
PS:我使用Python 2.7和pandas 0.20.3。
The seaborn.load_dataset
line detects the month
column as a category
data type. seaborn.load_dataset
行将month
列检测为category
数据类型。 To get around this error, cast categorical
to str
with this line right after flights = seaborn.load_dataset("flights")
: 要解决此错误,请在
flights = seaborn.load_dataset("flights")
之后立即将以下categorical
flights = seaborn.load_dataset("flights")
为str
:
flights["month"] = flights["month"].astype(str)
To sort the month strings in chronological order, first drop the top level (level=0) of the columns of flights_unstacked
(this level holds the single value passengers
): 要按时间顺序对月份字符串进行排序,请首先放下
flights_unstacked
列的最高级别(级别= 0)(此级别包含单个值passengers
):
import seaborn
import pandas as pd
flights = seaborn.load_dataset("flights")
flights["month"] = flights["month"].astype(str)
flights_indexed = flights.set_index(["year", "month"])
flights_unstacked = flights_indexed.unstack()
flights_unstacked.columns = flights_unstacked.columns.droplevel(0)
Then reindex the month-string columns according to a list of month strings that you pre-built in chronological order: 然后根据您按时间顺序预先构建的月份字符串列表重新索引月份字符串列:
import calendar
months = [calendar.month_name[i] for i in range(1, 13)]
flights_unstacked = flights_unstacked[months]
Finally, you can add a column of totals: 最后,您可以添加总计列:
flights_unstacked["Total"] = flights_unstacked.sum(axis=1)
Result: 结果:
In [329]: flights_unstacked
Out[329]:
month January February March April May June July August September October November December Total
year
1949 112 118 132 129 121 135 148 148 136 119 104 118 1520
1950 115 126 141 135 125 149 170 170 158 133 114 140 1676
1951 145 150 178 163 172 178 199 199 184 162 146 166 2042
1952 171 180 193 181 183 218 230 242 209 191 172 194 2364
1953 196 196 236 235 229 243 264 272 237 211 180 201 2700
1954 204 188 235 227 234 264 302 293 259 229 203 229 2867
1955 242 233 267 269 270 315 364 347 312 274 237 278 3408
1956 284 277 317 313 318 374 413 405 355 306 271 306 3939
1957 315 301 356 348 355 422 465 467 404 347 305 336 4421
1958 340 318 362 348 363 435 491 505 404 359 310 337 4572
1959 360 342 406 396 420 472 548 559 463 407 362 405 5140
1960 417 391 419 461 472 535 622 606 508 461 390 432 5714
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.