I have searched a lot about this topic of how to remove the index column of the datframe when exporting the dataframe to csv file. I found many solutions and tricks but none of them worked with me. In the csv output I got 0 (the column index) at the first row of the CSV output. Can you guide me of fixing such a problem?!!
2 Answers
Are you using pandas? If so, you can use the .to_csv method with the argument index=False to remove index:
df.to_csv('output.csv', index=False)
or header=False to remove column names:
df.to_csv('output.csv', header=False)
5 Comments
YasserKhalil
Thanks a lot. This was my first try and this removes the row's index not the column index.
YasserKhalil
Thanks a lot for your support.
alper
Does this print the results into terminal?
Flavio Moraes
@alper it doesn't. It only creates a csv file. If you want to print something you have to say it to print whatever you want to print.
alper
Can I redirect it to the terminal in paralel ? or should I do
cat output.csv' inside the python script?
df.to_csv(index=False)?df.reset_index(inplace=True)before doingdf.to_csv(index=False)?