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
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With