Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: cannot insert an item into a CategoricalIndex that is not already an existing category

I'm trying to create a new column in a pandas data frame representing the sum of each row (in this case, this number represents the number of passengers in a particular year from the Seaborn Flights dataset that comes with the library upon import. Here is my code:

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)

I'm receiving a few key errors:

KeyError: 'total' During handling of the above exception, another exception occurred:...

I believe the "key" error (pun intended) is the Type Error I also receive:

TypeError: cannot insert an item into a CategoricalIndex that is not already an existing category

like image 616
Ciryanroth Avatar asked Mar 09 '20 06:03

Ciryanroth


Video Answer


1 Answers

There is CategoricalIndex, so error, because total is not exist in categories.

Possible solution is convert columns to strings:

flights_unstacked.columns = flights_unstacked.columns.astype(str)
flights_unstacked['total'] = flights_unstacked.sum(axis=1)

Or add category by CategoricalIndex.add_categories:

flights_unstacked.columns = flights_unstacked.columns.add_categories('total')
flights_unstacked['total'] = flights_unstacked.sum(axis=1)
like image 131
jezrael Avatar answered Oct 10 '22 15:10

jezrael