我有一个包含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
如何从句法上解决这个问题?