【发布时间】:2020-05-21 01:28:55
【问题描述】:
类似Excel VLOOKUP,如何比较两个csv文件中的列并提取值?
a.csv
name,type
test1,A
test2,B
test3,A
test4,E
test5,C
test6,D
b.csv
type,value
A,1.0
B,0.5
C,0.75
D,0.25
“类型列”比较后的预期输出,使用这些值创建一个新的 csv 文件
newfile.csv
name,type,value
test1,A,1.0
test2,B,0.5
test3,A,1.0
test4,E,N/A
test5,C,0.75
test6,D,0.25
到目前为止,代码如下
A = 'a.csv'
B = 'b.csv'
df_B = pd.read_csv(B)
with open(A, 'r') as reference:
with open('newfile.csv', 'w') as results:
reader = csv.reader(reference)
writer = csv.writer(results)
writer.writerow(next(reader, []) + ['value'])
for row in reader:
checkRecords = df_B.loc[df_B['type'] == row[1]]
#checkRecords_A = df_B[df_B.type == row[1]].iloc[0] # IndexError: index 0 is out of bounds for axis 0 with size 0
if checkRecords.empty:
value = 'N/A'
else:
value = checkRecords.value
print(value)
# This value have name and dtype which is not expected
writer.writerow(row + [value])
results.close()
【问题讨论】:
-
这更像是用字母加入列而不是比较