fedotsoldier
fedotsoldier

Reputation: 190

Pandas non correct saving to CSV

I have CSV file:

lang
12345,it
77777,en

The first line is headers. My table have one column lang. In each of the next lines there are two values: index and value for this index.

When I am reading this table with Pandas by pd.read_csv(path) I am getting the next DataFrame structure:

      lang
12345   it
77777   en

But when I am saving it back to CSV by df.to_csv(path) I am getting redundant , before the headers in my CSV file:

,lang
12345,it
77777,en

It seems like pandas processes this comma like unnamed column, and when I am reading this file next time I am getting this DataFrame structure:

   Unnamed: 0 lang
0       12345   it
1       77777   en

But I want to save the first column as indexes and others like common columns. How I can save DataFrame with indexes in the first column?

Upvotes: 0

Views: 278

Answers (1)

ParalysisByAnalysis
ParalysisByAnalysis

Reputation: 733

First line mimics your df example where 12345 is in the index. Then I save that as a normal csv. Upon reading it back in, simply set index_col=0 to point pandas to read column at position 0 as the index.

df.set_index(df.columns[0],inplace=True)

df.to_csv('test.csv')
df_new = pd.read_csv('test.csv', index_col=0)

Upvotes: 1

Related Questions