我的CSV数据集Scrips.csv如下:
Code,Id
500002,ABB
500003,AEGISLOG
500004,TPAEC
500005,AKARLAMIN
500006,ALPHADR
500008,AMARAJABAT
500009,AMBALALSA
500010,HDFC
500011,AMRTMIL-BDM
500012,ANDHRAPET
500013,ANSALAPI
我想接受用户输入的字符串形式,并将其与“ Id”列匹配。我还想查看最接近的匹配项(如果没有完全匹配项),并要求用户输入这些匹配项之一。比赛结束后,我要返回对应于ID的“代码”。
我想要的输出是:
Enter the Id: ABB
500002
使用以下代码我无处可去:
import pandas as pd
from difflib import get_close_matches
df = pd.read_csv(r"C:\Users\fc\BSE Scraper\Scrips.csv", index_col=0)
for row in df.index:
if row == "ABB":
print("True")
elif len(get_close_matches(row, df.index())) > 0:
print("Did you mean %s instead?" % get_close_matches(row, df.index())[0])
else:
print("No match found. Please try again.")
答案 0 :(得分:2)
您可以将自定义函数与将Id
列转换为index
和f-string
一起使用:
def func(df, x):
df = df.set_index('Id')
m = df.index == x
if m.sum() > 0:
a = df.loc[x, 'Code']
return f'Exact match: {a}'
else:
val = get_close_matches(x, df.index)
if len(val) > 0:
a = df.loc[val[0], 'Code']
return f'Did you mean {val} instead for match {a}? '
else:
return "No match found. Please try again."
print (func(df, 'ABB'))
Exact match: 500002
print (func(df, 'ABB1'))
Did you mean ABB instead for match 500002?
print (func(df, 'something'))
No match found. Please try again.
答案 1 :(得分:0)
我不知道您认为类似ID的标准是什么。
但是我创建了这样的一些逻辑:
def get_close_matches(string):
global df
get_sr2 = df['Id'][df['Id'].str.startswith(string[:2])]
if len(get_sr2) !=0:
return get_sr2.tolist()
else:
return ''
while True:
get_id = input('Enter the Id : ')
get_sr = df['Code'][df['Id'].isin([get_id])]
if len(get_sr) != 0:
print(get_sr.iloc[0])
break
elif get_close_matches(get_id):
print("Do you mean one of the following?")
print(get_close_matches(get_id),'\n')
continue
else:
print("No match found. Please try again.")
continue
Enter the Id : AB
Do you mean one of the following?
['ABB']
Enter the Id : ABB
500002