使用Python计算从一张图纸到另一张图纸的值的匹配百分比



处理Excel文件,并通过使用数据帧匹配另一张表中的值来查找准确率百分比。使用具有唯一值的一列匹配其他列值。

我尝试过使用模糊匹配/任何其他可能的方法,但没有成功

输入数据:表1:

identity_no  address            Pincode   company_name
IN2231      Delhi, Indi        110030    AXN pvt Ltd
UK654       London, Uk         897653    Aviva Intl Ltd
SL1432      Colombo, Srilanka  07658     Ship Incorporations
LK0678      Libya, Sns         5674332   Oppo Mobiles pvt ltd

主数据表2

identity_no  address            Pincode   company_name
IN2231      Delhi, India       110030    AXN pvt Ltd
UK654       London, Uk         897653    Aviva Intl Ltd
SL1432      Colombo, Srilanka  07658     Ship Incorporations

预期输出:

identity_no  address            Pincode   company_name               match_percent

IN2231      Delhi, Indi        110030    AXN pvt Ltd                
UK654       London, Uk         897653    Aviva Intl Ltd
SL1432      Colombo, Srilanka  07658     Ship Incorporations
LK0678      Libya, Sns         5674332   Oppo Mobiles pvt ltd

到目前为止我尝试过的代码:

df = pd.read_excel(open(r'input.xlsx', 'rb'), sheet_name='sheet1')
df2 = pd.read_excel(open(r'master_data.xlsx', 'rb'), sheet_name='sheet2')
for index, row in df.iterrows():
for index_config, val_new in df2.iterrows():
if row['identity_no  '] == row_config['identity_no']:
df[['identity_no','address', 'Pincode', 'company_name']][Index] = val_config[['identity_no','address', 'Pincode', 'company_name']]

这里将值从sheet2映射到sheet1,但我也希望了解列匹配的准确性。

任何建议。

因此,如果我理解正确,您有一个数据帧,其中包含一些数据df,您希望将其与模板df2中的索引进行匹配,对于每个匹配的索引,您希望计算相似的元素数量。

# For simplicity, let's define the index of the dataframes
df = df.set_index("identity_no")
df2 = df2.set_index("identity_no")
# You define a function that returns NaN when index does not exist and the accuracy score if it does (from 0 to 1)
def accuracy_score(row):
if row not in df2.index:
return float("nan")
return sum(row[col] == df2.loc[row.name, col] for col in row.index) / len(row)
# You apply the function to your dataframe
df["accuracy"] = df.apply(accuracy_score, axis=1)

相关内容

最新更新