The incident happened when I simply performed CSV writing → reading in Python's pandas as follows.
import pandas as pd
#Define variable data and write to CSV file
data = pd.DataFrame({'name': ['Taro', 'Hanako', 'Jiro', 'Yuki'],
'Math': [80, 15, 90, 50],
'English': [80, 70, 50, 65],
'National language': [ 90, 60, 60, 60]})
print(data)
data.to_csv('a.csv')
#Read and output the exported CSV file
data = pd.read_csv('a.csv')
print(data)
When the above was executed, there was a difference in the output of each.
#First output result
name math English national language
0 Taro 80 80 90
1 Hanako 15 70 60
2 Jiro 90 50 60
3 Yuki 50 65 60
#Second output result
Unnamed:0 name math English national language
0 0 Taro 80 80 90
1 1 Hanako 15 70 60
2 2 Jiro 90 50 60
3 3 Yuki 50 65 60
A mysterious column called ʻUnnamed: 0` has been added. Eliminate this.
In the above writing, the following CSV file was output.
,name,Math,English,National language
0,Taro,80,80,90
1,Hanako,15,70,60
2,Jiro,90,50,60
3,Yuki,50,65,60
An unexpected ,
has been added to the left of name on the first line.
As a result, it seems that the first column is regarded as bearer.
Either of the following can be used. You don't have to do both.
It can be solved by specifying index as false as shown below.
data.to_csv('a.csv', index=False)
From the above, the following CSV file could be output.
name,Math,English,National language
0,Taro,80,80,90
1,Hanako,15,70,60
2,Jiro,90,50,60
3,Yuki,50,65,60
read_csv
Specify the index column as follows.
data = pd.read_csv('a.csv', index_col=0)
Recommended Posts