pythonpandasdataframepercentageaccumulate

2 Different Data Frames + Percentage Calculation + Python


there are similar questions existing, however cant find the right answer. Most of them require a common nominator which I don't have.

I want to have two outcomes from two data frames.

One is to get the percentage for each row in df2 from the total (df1). And another view of accumulated percentage.

df1
a
1875


df2
b   c
aaa 125
bbb 250
ccc 500
ddd 1000


Required outcome. 

b   c   Outcome 1   Outcome 2
aaa 125     6.67%       100.00%
bbb 250    13.33%        93.33%
ccc 500    26.67%        80.00%
ddd 1000   53.33%        53.33%

I have tried df1.eq(df2.values).mean() and couple of the merge functions. But again, don't have a common nominator.

Hope this helps. Thanks.


Solution

  • Use:

    #get scalar from first DataFrame
    a = df1.loc[0, 'a']
    
    #divide by scalar and multiple by 100
    df['Outcome 1'] = df['c'].div(a).mul(100)
    #create cumulative sum in swapped order of rows
    df['Outcome 2'] = df['Outcome 1'].iloc[::-1].cumsum()
    print (df)
         b     c  Outcome 1   Outcome 2
    0  aaa   125   6.666667  100.000000
    1  bbb   250  13.333333   93.333333
    2  ccc   500  26.666667   80.000000
    3  ddd  1000  53.333333   53.333333