Python 计算每行项目不含增值税的税率的程序

Python 计算每行项目不含增值税的税率的程序,python,pandas,dataframe,numpy,Python,Pandas,Dataframe,Numpy,假设您拥有以下df: d = {'line amount#1': [5.95], 'line amount#2': [5.95], 'line amount#3': [15.75],'line amount#4': [15.75], 'line amount#5': [3.9] ,'line amount#6': [2.9], 'line amount#7': [np.nan], 'line amount#8': [np.nan], 'line amount#9': [np.nan],'

假设您拥有以下df:

d = {'line amount#1': [5.95], 'line amount#2': [5.95], 'line amount#3': [15.75],'line amount#4': [15.75], 'line amount#5': [3.9]
    ,'line amount#6': [2.9], 'line amount#7': [np.nan], 'line amount#8': [np.nan], 'line amount#9': [np.nan],'line amount#10': [np.nan]
    , 'BTW':[5.85],'ExclVAT':[44.35], 'Totaal': [50.2]}
dfcalc = pd.DataFrame(data=d)
dfcalc 
仅显示第1行和第10行金额的表格:

从这个DF中,我想从每个特定行的税率中提取税率。应动态计算税率行项目,以便行金额除以某个税率后,当行金额相加时,合计不含税金额。税率可以是0.09、0.21和0.00,并且应该等于增值税(BTW)子集。我尝试了以下方法:

from itertools import product

# get all possible tax rate combinations
x = [1.09, 1.21, 0.00]
combinations = np.array(list(product(*[x]*10)))

# get amount columns
amounts = dfcalc.filter(like='line amount')

# calculate total VAT for each row for each tax rate combination
vats = amounts.fillna(0).dot(combinations.T).round(1)


# for each row find the combination that gives total VAT
# that is equal to the value in VAT column for that row
ix = vats.eq(dfcalc['ExclVAT'].round(1), axis=0).idxmax(axis=1)
taxrates = np.where(amounts.notna(), combinations[ix], np.nan)
@perl回答了这个问题的一个变体: 然而,我不会再继续下去了

期望产出是行金额除以等于不含增值税的税率:

+----+-----------------+------------------+-------+-----------+----------+
|    |   line amount#1 |   line amount#2  |   BTW |   ExclVAT |   Totaal |
|----+-----------------+------------------+-------+-----------+----------|
|  0 |            4.92 |              4.92|  5.85 |     44.35 |     50.2 |
+----+-----------------+------------------+-------+-----------+----------+

请帮忙

四舍五入有点问题,没有一个税率组合能给出准确的答案

我已经更新了代码,以找到能够为我们提供最准确数字的组合:

from itertools import product

# get all possible tax rate combinations
x = [0.00, 0.09, 0.21]
combinations = np.array(list(product(*[x]*10)))

# get amount columns
amounts = dfcalc.filter(like='line amount')

# calculate amounts excluding VAT for each row for each tax rate combination
exclvat = amounts.fillna(0).dot((1 + combinations.T)**-1)

# for each row find the combination that gives amounts excluding VAT
# that is equal to the value in ExclVAT column for that row
ix = np.abs(exclvat.sub(dfcalc['ExclVAT'].squeeze(), 0)).idxmin(1)
taxrates = np.where(amounts.notna(), combinations[ix], np.nan)

# subtract tax from line amounts
dfcalc[amounts.columns] /= (1 + taxrates)
dfcalc['line amount sum'] = dfcalc.filter(like='line amount').sum(1)
dfcalc.T
输出:

                         0
line amount#1     4.917355
line amount#2     4.917355
line amount#3    14.449541
line amount#4    14.449541
line amount#5     3.223140
line amount#6     2.396694
line amount#7          NaN
line amount#8          NaN
line amount#9          NaN
line amount#10         NaN
BTW               5.850000
ExclVAT          44.350000
Totaal           50.200000
line amount sum  44.353628

谢谢你!你帮了大忙。我祝你度过一个愉快而富有成果的一天,谢谢你,MaxOK,所以这可能是关于
ix
ix
看起来像什么?(可能只是
ix.head()
的输出)好的,看起来不太好。。。让我们尝试将当前的
ix=…
替换为
ix=np.abs(exclavat.sub(dfcalc['exclavat'].squeak(),0)).idxmin(1)
(在我上面的回答中进行了更新,因此您可以在需要时复制完整的代码)谢谢您!这很好地工作:ix.head()现在变成:0 1045845 1 1048405 2 1048565 3 349525 5 349525数据类型:int64很酷,很高兴它有帮助!不过,您会得到
1045845
,这有点令人惊讶。您有多少笔
行金额
s?(
ix
对于10行金额,最多应为
59049
                         0
line amount#1     4.917355
line amount#2     4.917355
line amount#3    14.449541
line amount#4    14.449541
line amount#5     3.223140
line amount#6     2.396694
line amount#7          NaN
line amount#8          NaN
line amount#9          NaN
line amount#10         NaN
BTW               5.850000
ExclVAT          44.350000
Totaal           50.200000
line amount sum  44.353628