Home > Blockchain >  Pandas is adding an index column even though I have specified "index_col = False", and is
Pandas is adding an index column even though I have specified "index_col = False", and is

Time:03-24

As the title describes, Pandas is adding an extra column called "index" which it is then unable to find when I attempt to drop it. The CSV that I am reading in does not have an index column, but seeing as one is added when I use read_csv, I thought it would be sensible to try index_col = False

Reading in code:

df = pd.read_csv("file.csv", usecols=["longitude", "latitude", "id", "gross_premise_area", "build_replacement_cost", "structure_cost", "content_cost"], index_col=False)

Which results in this:

duplicate index column

So I then try to remove the extra column:

df.drop("index", axis=1, inplace=True)

Which results in the following error:

Exception has occurred: KeyError "['index'] not found in axis"

Example of CSV:

map_use,premise_area,premise_floor_count,longitude,latitude,geomni_premise_id,geomni_building_id,premise_age,use,age_category,area,floors,zone,build_replacement_cost,gross_premise_area,structure_cost,content_cost,geom_col_vulnerabilities,id Unclassified,86,NULL,-64.8,31.8,1234,1234,Unknown date,commercial,post-1985,86,2,city,2459,172,593,593,1234,3254654363634554654645645654

CodePudding user response:

I couldn't reproduce the error with the details you have given. You could perhaps try with the following, either separately or together :

  1. Edit the Dataset in Excel/google sheet so that you only have the columns you need.
  2. Just get rid of index_col = False, because the default is None anyway.

CodePudding user response:

There must be an index while using the data frame. If you set it to false, Pandas will add a numerical index for you.

If you don't want it while saving the data frame to a file, use pd.to_csv(path, index=False).

If you want to have another column as the index, use index_col= int / str

The drop you're trying to use means drop a column that is called "index", and you already don't have one.

  • Related