Will there be a conflict between names and usecols of read_csv?

Viewed 35

I have several csv files and want to extract the third column of all the files into another csv file。But I had a problem。 All csv files as below. It's just that the values of the third column are different

test_indexs,test_labels,predicts_labels
0,0,57
1,0,83
2,0,0
3,0,70
4,0,0
5,0,0

My extraction code:

    for i in range(10):
          real_label = pd.read_csv(path+str(i)+".csv",
                                     header=0, names=[i], usecols=[2])

The extracted data is as follows:

0,1,2,3,4
57,83,0,0,0
83,0,1,0,83
0,0,2,0,83
70,70,3,92,45
0,0,4,0,0
0,0,5,0,0
0,0,6,0,0
0,0,7,0,0
0,0,8,0,0
0,0,9,57,0
57,57,10,57,57

The first line is header。 The extracts of the other columns are all correct, but not correct when extracting the third column of the 3rd csv file。That's the third column above

The third csv file is as follows:

test_indexs,test_labels,predicts_labels
0,0,0
1,0,0
2,0,0
3,0,70
4,0,0
5,0,0
6,0,0
7,0,92
8,0,0

It looks like I didn't succeed in extracting the third column of the original file, but instead extracted the first column of the original file。 Is this because there is a conflict between names and usecols when I use read_csv

1 Answers

Finally,I used a non-clever approach

for i in range(10):
          real_label = pd.read_csv(path+str(i)+".csv",
                                     header=0, names=[i], usecols=[2])
          if sort_csv_i == 2:
                real_label[2] = pd.read_csv(path+str(i)+".csv",
                                        usecols=[2])

This will get the results I want, although I don't know why

Related