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"
set_indexcall when usinginplace=Truewill invalidatedf_car_index(it will beNone) and the reference to the dataframe will be lostprint(df_car_index.columns)and see if it has a columnhorsepower(according to the error message, it does not).horsepowercolumns, but it is not there in the output you provided (please also add it to the question). That is exactly the problem.