Saving and loading data in csv results in "Nameless" columns

prob in title. exaple:

x=[('a','a','c') for i in range(5)] df = DataFrame(x,columns=['col1','col2','col3']) df.to_csv('test.csv') df1 = read_csv('test.csv') Unnamed: 0 col1 col2 col3 0 0 aac 1 1 aac 2 2 aac 3 3 aac 4 4 aac 

The reason is that when you save the data frame, the index column is also indicated, with no name in the header. then when csv loads, the index column is loaded again as an unnamed column. This is mistake? How can I avoid writing csv with index or deleting unnamed columns while reading?

+6
source share
4 answers

You can remove row labels using the index and index_label to_csv options .

+7
source

They are not symmetrical, because in the csv format there are ambiguities due to positioning. You need to specify index_col when reading

 In [1]: x=[('a','a','c') for i in range(5)] In [2]: df = DataFrame(x,columns=['col1','col2','col3']) In [3]: df.to_csv('test.csv') In [4]: !cat test.csv ,col1,col2,col3 0,a,a,c 1,a,a,c 2,a,a,c 3,a,a,c 4,a,a,c In [5]: pd.read_csv('test.csv',index_col=0) Out[5]: col1 col2 col3 0 aac 1 aac 2 aac 3 aac 4 aac 

This looks very similar to the above, since the "foo" column or index?

 In [6]: df.index.name = 'foo' In [7]: df.to_csv('test.csv') In [8]: !cat test.csv foo,col1,col2,col3 0,a,a,c 1,a,a,c 2,a,a,c 3,a,a,c 4,a,a,c 
+4
source

You can specify explicitly which columns you want to write using the cols parameter.

0
source

Here's how to use the df.to_csv('test.csv', index_label=False) index df.to_csv('test.csv', index_label=False) But for me, when I tried to present Kaggle, it returns the error "ERROR: Record 1 has 3 columns, but 2 is expected," so I decided to use this code .

0
source

Source: https://habr.com/ru/post/956172/


All Articles