The Python Oracle

Removing index column in pandas when reading a csv

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Hypnotic Puzzle2

--

Chapters
00:00 Removing Index Column In Pandas When Reading A Csv
00:39 Accepted Answer Score 89
01:06 Answer 2 Score 7
01:28 Answer 3 Score 389
01:52 Answer 4 Score 21
02:05 Thank you

--

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

--

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))