The Python Oracle

Removing index column in pandas when reading a csv

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Darkness Approaches Looping

--

Chapters
00:00 Question
00:54 Accepted answer (Score 88)
01:27 Answer 2 (Score 366)
01:58 Answer 3 (Score 124)
02:13 Answer 4 (Score 21)
02:33 Thank you

--

Full question
https://stackoverflow.com/questions/2010...

Accepted answer links:
[10 minutes to Pandas]: http://pandas.pydata.org/pandas-docs/sta...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #pandas

#avk47



ANSWER 1

Score 389


When writing to and reading from a CSV file include the argument index=False and index_col=False, respectively. Follows an example:

To write:

 df.to_csv(filename, index=False)

and to read from the csv

df.read_csv(filename, index_col=False)  

This should prevent the issue so you don't need to fix it later.




ACCEPTED ANSWER

Score 89


DataFrames and Series always have an index. Although it displays alongside the column(s), it is not a column, which is why del df['index'] did not work.

If you want to replace the index with simple sequential numbers, use df.reset_index().

To get a sense for why the index is there and how it is used, see e.g. 10 minutes to Pandas.




ANSWER 3

Score 21


You can set one of the columns as an index in case it is an "id" for example. In this case the index column will be replaced by one of the columns you have chosen.

df.set_index('id', inplace=True)



ANSWER 4

Score 7


If your problem is same as mine where you just want to reset the column headers from 0 to column size. Do

df = pd.DataFrame(df.values);

EDIT:

Not a good idea if you have heterogenous data types. Better just use

df.columns = range(len(df.columns))