Reputation: 127
I have created the original dataframe from a CSV file
df = pd.read_csv(r'C:\Users\Sam\cars.csv')
Which produces a df with the following columns
Index(['mpg', 'cylinders', 'displacement', 'horsepower', 'weight',
'acceleration', 'model_year', 'origin', 'name'],
dtype='object')
I can set one of these columns as an index
df.set_index('cylinders')
I then successfully created a copy of this original df and inserted a combined name year column
df_name = df.copy()
df_name ['name_year'] = df.name + ' - 19' + df.model_year.astype(str)
However, whenever trying to assign a column as an index, be it the new name_year column or otherwise, I am met with the same keyError message
df_car_index = df_name.copy()
df_car_index = df_car_index.set_index('horsepower', inplace=True)
df_car_index
KeyError: "None of ['horsepower'] are in the columns"
Upvotes: 9
Views: 19360
Reputation: 1
I ran into this issue with Kaggle (similar to Jupyter) and solved it by restarting/clearing the outputs and running all of the code over again. Instead of deleting cells I no longer needed, I was cutting the cells so code that I thought were gone still remained in the memory, messing with my updated code.
Upvotes: 0
Reputation: 531
When you set the column as an index, it is moved to index and removed from columns. Thus set_index()
only needs to be run once when you pass inplace=True
, making it a permanent change.
If you want to revert the index change, you can do, df.reset_index(inplace=True)
. The column that was moved to index will be re-added to the columns. Like set_index()
, this code needs to be run only once, re-running it will cause errors.
You can find a demonstration of how to set a custom index and revert it back on this tutorial by Data School.
Another important point is: If you try to add another index over the custom index that was set from one of the columns, it overwrites the custom index column, and effectively deletes it.
Upvotes: 5
Reputation: 1
You need to run that part of the code just once. When that column is set as an index and you run the code again, you will get the error.
Upvotes: -1