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

Validation check Excluding VAT - VAT and Total Amount

我正在尝试 reconciliation/validation 检查两个数字是否等于总数,一个不包括增值税,一个增值税。

我有以下 df:

    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'

请帮忙

您可以使用 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)

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)