简体   繁体   English

如何在 python 的条件下执行脚本

[英]How to execute the script within a condition in python

I am looking to execute the script only when it satisfies the condition.我希望仅在满足条件时才执行脚本。

If Column1 is not blank then only we can use the below script else will print the message.如果 Column1 不是空白,那么只有我们可以使用下面的脚本,否则将打印消息。 I have tried several ways but couldn't find the possible way to work.我尝试了几种方法,但找不到可能的工作方式。

Sheet1表 1

id_number  company_name        match_acc

IN2231D    AXN pvt Ltd
UK654IN    Aviva Intl Ltd
           Ship Incorporations
LK0678G    Oppo Mobiles pvt ltd
NG5678J    Nokia Inc

Sheet2表 2

identity_no   Pincode   company_name

 IN2231        110030    AXN pvt Ltd
 UK654IN       897653    Aviva Intl Ltd
 SL1432        07658     Ship Incorporations
 LK0678G       120988    Oppo Mobiles Pvt Ltd

Script i have been using我一直在使用的脚本

df1 = pd.read_excel(open(r'input.xlsx', 'rb'), sheet_name='sheet1')
df2 = pd.read_excel(open(r'input.xlsx', 'rb'), sheet_name='sheet2')

if df1[['id_number']] is not NaN:
  cross = df1[['id_number']].merge(df2[['identity_no']], how='cross')
  cross['match_acc'] = cross.apply(lambda x: fuzz.ratio(x.id_number, x.identity_no), axis=1)
  df1['match_acc'] = df1.id_number.map(cross.groupby('id_number').match_acc.max()) 

How we can execute it if i try using it within a function:如果我尝试在 function 中使用它,我们将如何执行它:

def word(x,y):
   cross = df1[['id_number']].merge(df2[['identity_no']], how='cross')
   cross['match_acc'] = cross.apply(lambda x: fuzz.ratio(x.id_number, y.identity_no), axis=1)
   df1.id_number.map(cross.groupby('id_number').match_acc.max()) 

df['match_acc'] = df1.apply(lambda x:word if (x['id_number'] == 'NaN') else 'No',1)

Please suggest请建议

Is this what you're looking for这是你要找的吗

def word(df1):
   cross = df1[['id_number']].merge(df2[['identity_no']], how='cross')
   cross['match_acc'] = cross.apply(lambda x: fuzz.ratio(x.id_number, x.identity_no), axis=1)
   return df1.id_number.map(cross.groupby('id_number').match_acc.max()) 

df['match_acc'] = df1.apply(lambda x:word(x) if (x['id_number'] == 'NaN') else 'No',axis=1)

I'm not sure how your end result is supposed to look, but I assume you want to merge df1 and df2 based on a fuzzy match on id_number/ identity.我不确定你的最终结果应该是什么样子,但我假设你想基于 id_number/ 身份的模糊匹配来合并 df1 和 df2 。

Try these related answers that merge Pandas dataframes with fuzzy logic:尝试这些将 Pandas 数据帧与模糊逻辑合并的相关答案:

Fuzzy matching to join two dataframe 模糊匹配加入两个dataframe

or try the example below from another SO answer.或从另一个 SO 答案尝试以下示例。 This might solve your problem without additional complex code.这可能无需额外的复杂代码即可解决您的问题。

import pandas as pd
import fuzzy_pandas as fpd

df1 = pd.DataFrame({'Key':['Apple', 'Banana', 'Orange', 'Strawberry']})
df2 = pd.DataFrame({'Key':['Aple', 'Mango', 'Orag', 'Straw', 'Bannanna', 'Berry']})

results = fpd.fuzzy_merge(df1, df2,
            left_on='Key',
            right_on='Key',
            method='levenshtein',
            threshold=0.6)

results.head()
  Key    Key
0 Apple  Aple
1 Banana Bannanna
2 Orange Orag

Source: https://stackoverflow.com/a/60634160/13530653资料来源: https://stackoverflow.com/a/60634160/13530653

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM