Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dataframe to csv missing column name for index

Tags:

python

pandas

When writing the pandas mainTable dataframe to mainTable.csv, but after the file is written the name of the column for index is missing.
Why does this happen since I have specified index=True?

mainTable.to_csv(r'/Users/myuser/Completed/mainTable.csv',index=True)
mainTable = pd.read_csv('mainTable.csv')
print(mainTable.columns)


 MacBook-Pro:Completed iliebogdanbarbulescu$ python map.py
Index(['Unnamed: 0', 'name', 'iso_a3', 'geometry', 'iso_code', 'continent']

print output

enter image description here

like image 621
bibscy Avatar asked Mar 19 '26 13:03

bibscy


1 Answers

save with index_label='Index_name', since by default index_label=None.
See for pandas' .csv() method : https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.to_csv.html

mainTable.to_csv(r'/Users/myuser/Completed/mainTable.csv',index=True, index_label='Index_name')
like image 94
imdevskp Avatar answered Mar 22 '26 04:03

imdevskp