![](/img/trans.png)
[英]Is there a way in Python to calculate one time as a percentage of another?
[英]Calculate Match percentage of values from One sheet to another Using Python
处理 Excel 文件,并通过使用 dataframe 匹配另一张表中的值来查找准确度百分比。 使用具有唯一值的一列匹配其他列值。
我尝试过使用模糊匹配/任何其他可能的方法,但没有奏效
输入数据:表 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
预期 Output:
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,但我也希望找出列匹配的准确度。
有什么建议么。
因此,如果我理解正确,您有一个 dataframe 和一些数据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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.