1

If while reading a CSV using pd.read_csv('xyz.csv', index_col =0 ), putting index_col = 0 prohibits me from dropping the very next column in the dataframe. I am able to use df.drop() for the other columns in the dataframes but not the one which takes the place of index after using index_col = 0

I get a KeyError which tells me that the column I want to drop does not exist.


import pandas as pd
import numpy as np
df = pd.read_csv('kaggle_houseprice_train.csv', index_col=0) 

# dataset from https://www.kaggle.com/c/house-prices-advanced-regression-techniques/data

df.drop(['id'], 1, inplace = True) # does not work unless I remove index_col = 0 from the read_csv step

print(df.head())

Error that occurs :

KeyError: "['id'] not found in axis"

But this column does exist in the CSV!

molbdnilo
  • 64,751
  • 3
  • 43
  • 82
  • 1
    What does df.head() show? id is probably the index, you might wanna try `df.reset_index(drop=True)` – ayhan May 26 '19 at 11:47

0 Answers0