9

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"
6
  • 1
    Unrelated, but assigning back the set_index call when using inplace=True will invalidate df_car_index (it will be None) and the reference to the dataframe will be lost Commented Oct 22, 2020 at 23:07
  • 1
    Anyway, try to print(df_car_index.columns) and see if it has a column horsepower (according to the error message, it does not). Commented Oct 22, 2020 at 23:08
  • Thank you for your reply It does appear to have horsepower as a column df_car_index = df_name.copy() print(df_car_index.columns) Index (['mpg', 'cylinders', 'displacement', 'weight', 'acceleration', 'model_year', 'origin', 'name', 'name_year'], dtype = 'object') Commented Oct 22, 2020 at 23:10
  • 1
    I'm confused. You wrote that it does appear to have horsepower columns, but it is not there in the output you provided (please also add it to the question). That is exactly the problem. Commented Oct 22, 2020 at 23:17
  • 1
    I have no idea what we did but it appears to be working now, I can return any column as index, including name year Commented Oct 22, 2020 at 23:27

3 Answers 3

5

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.

Sign up to request clarification or add additional context in comments.

2 Comments

"set_index() only needs to be run once" - OP's code only runs it once, so what makes you think OP is running it more than once? I guess if they're re-running a Jupyter cell without thinking, that could do it, but it's only a guess.
"[df.reset_index(inplace=True)] needs to be run only once, re-running it will cause errors" - You can run it three times for a df with a custom index. After that, you'll get ValueError: cannot insert level_0, already exists, since it's trying to insert the default RangeIndex as a column but the default column name index already exists and so does the fallback name level_0.
0

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.

1 Comment

I suppose another fix is to close out of everything and reopening it, which may be what you did to solve your issue.
-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.

1 Comment

Consider adding some code to your answer to explain what "that" code is.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.