как разделить одну сумму на 5 продуктов по заданному проценту в фрейме данных - PullRequest
0 голосов
/ 02 августа 2020

У меня есть 2 кадра данных, как показано ниже: DF1 и DF2

DF1: я импортирую это из файла Excel.

 DF1 
  Product             % to be applied in Exp
0   00028                  0.1080
1   00031                  0.2067
2   00045                  0.3469
3   00089                  0.1877
4   00099                  0.1507
5   Total                  1.0000

enter image description here

DF2 : I am importing this from an Excel file which contains thousand of Expanses details. How ever i am only taking 3 rows for my calculation.

 DF2
   Account    CC Account Dis  Exp Amount
0   523761  US12   Admin Exp    50000.12
1   578212  9999  Travle Exp    89673.89
2   578511  0234       Rent     50202.99

Out put :I want out put as below. each row of Exp Amount of DF2 must be splited by each product by its % from DF1. Is there any way around to solve this calculations ?

Output

>>> Output
    Account    CC Account Dis  Product    Exp Amount
0    523761  US12   Admin Exp       28   5400.012960
1    523761  US12   Admin Exp       31  10335.024804
2    523761  US12   Admin Exp       45  17345.041628
3    523761  US12   Admin Exp       89   9385.022524
4    523761  US12   Admin Exp       99   7535.018084
5    578212  9999  Travle Exp       28   9684.780120
6    578212  9999  Travle Exp       31  18535.593063
7    578212  9999  Travle Exp       45  31107.872441
8    578212  9999  Travle Exp       89  16831.789153
9    578212  9999  Travle Exp       99  13513.855223
10   578511  0234       Rent        28   5421.922920
11   578511  0234       Rent        31  10376.958033
12   578511  0234       Rent        45  17415.417231
13   578511  0234       Rent        89   9423.101223
14   578511  0234       Rent        99   7565.590593

введите описание изображения здесь

1 Ответ

0 голосов
/ 02 августа 2020

Вы можете объединить две таблицы и умножить Exp Amount на % to be applied in Exp. Затем просто отбросьте ненужные столбцы.

df1.drop(df1.index[df1['Product'] == 'Total'], inplace = True)
df1['Product'] = pd.to_numeric(df1["Product"])
df1['key'], df2['key'] = 1,1
df3 = df2.merge(df1, on = 'key').drop('key', 1)
df3['Exp Amount'] = df3['Exp Amount'].multiply(df3['% to be applied in Exp'])
df3.drop('% to be applied in Exp', 1, inplace = True)
df3 = df3.reindex(columns = ['Account', 'CC', 'Account Dis', 'Product', 'Exp Amount'])
df3
>>>
    Account    CC Account Dis  Product    Exp Amount
0    523761  US12   Admin Exp       28   5400.012960
1    523761  US12   Admin Exp       31  10335.024804
2    523761  US12   Admin Exp       45  17345.041628
3    523761  US12   Admin Exp       89   9385.022524
4    523761  US12   Admin Exp       99   7535.018084
5    578212  9999  Travle Exp       28   9684.780120
6    578212  9999  Travle Exp       31  18535.593063
7    578212  9999  Travle Exp       45  31107.872441
8    578212  9999  Travle Exp       89  16831.789153
9    578212  9999  Travle Exp       99  13513.855223
10   578511  0234        Rent       28   5421.922920
11   578511  0234        Rent       31  10376.958033
12   578511  0234        Rent       45  17415.417231
13   578511  0234        Rent       89   9423.101223
14   578511  0234        Rent       99   7565.590593 
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...