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