Vlookup,在2个文件之间合并,选择2列

2024-05-19 10:22:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用pandas制作一个简单的python脚本,用于类似于excel的Vlookup

我有两个用制表符分隔的txt文件(即All.txt和Half.txt):

All.txt:

enter image description here

HALF.txt:

enter image description here

我需要的是用匹配的“Variant\u ID”来填补All.txt的空白(“All.tx文件的N_Casos\u LCR”和“Location\u LCR”)

所以我从All.txt设置了(或者我想我设置了)索引“Variant_ID”列,但是我得到了下一个错误

欢迎任何帮助或建议! 谢谢

输入代码:

import pandas as pd

falta=open('Half.txt','rb')
todo=open('All.txt','rb')

df1 = falta.readlines()
df2 = todo.readlines()

df2.set_index("Variant_ID",inplace=True)
df3 = df2.merge(df1, left_index=True, right_on="N_Casos_LCR", how='left')
df3.reset_index(inplace=True, drop=True)

输出:

Traceback (most recent call last):                                                                 
File "BuscarV2.py", line 9, in <module>                                                            
df2.set_index("Variant_ID",inplace=True)                                                     
AttributeError: 'list' object has no attribute 'set_index'

Tags: 文件txtidtruepandasindexalldf2

热门问题