简体   繁体   中英

Raise TypeError when adding new column to a pandas DataFrame

I've been watching an online course about data analysis using 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. 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.

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.

The seaborn.load_dataset line detects the month column as a category data type. To get around this error, cast categorical to str with this line right after flights = seaborn.load_dataset("flights") :

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 ):

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM