如何获取csv中元素的条目与文本文件中的条目匹配的元素的索引

我有一个包含ID的文本文件 abc.txt

301KG0KXAFQBZD5C6JXD5Y3V32D2HR
301KG0KXAFQBZD5C6JXD5Y3V32DH26
302OLP89E2C9N8P0X6CR0PPD2YZCAY
302U8RURK26C60PPXRC1CNX2MCEVN0
304QEQWK0SPEVKOLV9OP6J7HIQ70OT
306996CF7ZPUJFKUNNN3E4QSGJU1BT
306996CF7ZPUJFKUNNN3E4QSGJV1BU
306W7JMRZ13CUF4FM8WITED0UP08BK
307FVKVSZUKO92ENXOUP70BZ9DE74R
3087LXLJ7PLKP7BSW65ZJPY3QPFF0L
3087LXLJ7PLKP7BSW65ZJPY3QPHF0N

and so on

我要做的就是:

对于 abc.txt 中的每个字符串,找到并返回索引号 xyz.csv ,其索引名为 HITID 的列包含该字符串,并且将其分配给索引变量。

我的尝试

clm = pd.read_csv('xyz.csv')

f = open('abc.txt','rb')

for entry in f:
    # if clm['HITId'].str.contains(entry).any()

    index =clm[clm['HITId']==entry].index.item()
    print(index)

出现错误提示:

  File "approve_reject_hits_fingerD.py",line 89,in <module>
    index =clm[clm['HITId']==entry].index.item()
  File "/Users/AjayB/anaconda3/envs/MyDjangoEnv/lib/python3.6/site-packages/pandas/core/base.py",line 716,in item
    return self.values.item()
ValueError: can only convert an array of size 1 to a Python scalar

当我使用该行时:

index =clm[clm['HITId']==entry].index[0]

然后我想到这个:

  File "approve_reject_hits_fingerD.py",in <module>
    index =clm[clm['HITId']==entry].index[0]
  File "/Users/AjayB/anaconda3/envs/MyDjangoEnv/lib/python3.6/site-packages/pandas/core/indexes/base.py",line 3958,in __getitem__
    return getitem(key)
IndexError: index 0 is out of bounds for axis 0 with size 0

对python特别是pandas有所了解,因此尝试了以下尝试:

Find element's index in pandas Series

Get index of a row of a pandas dataframe as an integer

如何从句法上解决这个问题?

hzhz020261 回答:如何获取csv中元素的条目与文本文件中的条目匹配的元素的索引

没有匹配任何值的问题,因此出错。可能的解决方案是将nextiter一起使用,以返回第一个匹配的值(如果还存在其他默认值),此处为no match

clm = pd.read_csv('xyz.csv')

f = open('abc.txt','rb')

for entry in f:

    index =clm[clm['HITId']==entry].index
    print(index)
    print (next(iter(index),'no match'))
本文链接:https://www.f2er.com/3168385.html

大家都在问