验证检查,不包括增值税和总金额

2024-05-03 10:46:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试核对/验证两个数字,一个不含增值税,一个增值税等于总额

我有以下建议:

    Document Type   Factuurnummer   FactuurdatumKvK    ExclBTW   BTW        Totaal  Vervaldatum Item    Omschrijving    ... Betalingsvoorwaarden    Email   Postalcode_Finalp   Postalcodestringp   Cityp   Countryp    Postalcode_Final    Postalcodestring    City    Country
0   NaN 44  2021-02-27  58782494                       1700.00  357.00  2057.00 2021-03-13  

I've tried the following code:
#validation check
for i, row in df1.iterrows():
    if df1['Totaal'][i].astype('float') == (df1['ExclBTW'][i].astype('float') + df1['BTW'][i].astype('float')):
        df1['Totaal'].astype('float') == df1['Totaal'].astype('float')
    else:
        df1['Totaal'] = "ERROR!" 

但是,获取无效的ValueError。。你们认为什么是最好的

--------------------------------------------------------------------------- AttributeError                            Traceback (most recent call last) <ipython-input-14-a461c3c9030f> in <module>
    113 #validation check
    114 for i, row in df1.iterrows():
--> 115     if df1['Totaal'][i].astype('float') == (df1['ExclBTW'][i].astype('float') + df1['BTW'][i].astype('float')):
    116         df1['Totaal'].astype('float') == df1['Totaal'].astype('float')
    117     else:

AttributeError: 'str' object has no attribute 'astype'

请帮忙


Tags: inforifcheckfloat增值税rowvalidation
2条回答

Pandas允许您以矢量化方式执行这些操作

# have numpy for efficiency
import numpy as np

...
# assuming  no missing values
# np.where(condition, value_if_condition_true, value_if_condition_false, [default_value])

np.where(df1['Totaal'].astype('float') == (df1['ExclBTW'].astype('float') + df1['BTW'].astype('float')), True, False)

您可以使用astypeapply执行此操作:

df.loc[:, ['ExclBTW', 'BTW','Totaal']] = df[['ExclBTW', 'BTW','Totaal']].apply(pd.to_numeric, errors='coerce')

df['new_col'] = df[['ExclBTW', 'BTW','Totaal']].\
                  apply(lambda x: x['Totaal'] if x['Totaal'] == (x['ExclBTW'] + x['BTW']) else 'ERROR!', axis=1)

相关问题 更多 >