我必须使用CSV文件Book1和Book2。 Book1中的列为A, B, C, D, E
,而Book2中的列为A, B, E, H.
我想以这样的方式修改Book2:它只包含Book1中常见的列名以及Book1中的其他任何附加内容。文件是:
第一册
A B C D E
10.12.0.1 a 35 0 11
10.12.0.1 b 35 1 10
107.77.87 a 35 0 101
第二册:
A B E H
9.81.2 b 10 w
10.15.32 b 100 w
11.16.5 b 101 w
修改后,最终的Book2将是: Book2_final:
A B C D E
9.81.2 b 10
10.15.32 b 100
11.16.5 b 101
我尝试了以下内容:
import pandas
a= open('input_test.txt','r')
csv1 = pandas.read_csv('Book2.csv',dtype='unicode')
inserted_cols = a.read().split(',')
csv1[inserted_cols].to_csv('Book2_test.csv',index=False)
其中文件'input_test.txt'
包含以逗号分隔的相同顺序的Book1列。但我得到错误:
KeyError: "['C' 'D' 'E\\n'] not in index"
无法弄清楚什么是错的。新的Python。