Temp_coder
Temp_coder

Reputation: 287

Calculation on each cell of a dataframe

I have two dataframes.

data1 =

Variables   A_0      B_0
Bins                                    
1           200      100
2           100      150
3           100      300
4           100      200
5           150      100 

data2 =

Variables   A_1      B_1
Bins                                    
1           50       100
2           100      150
3           50       50
4           100      200
5           100      100 

Now I want to create a dataframe that gives the percentage of cell values in data2, say for A_1, Bins = 1 has 50 value and for A_0, Bins=1 has 200 value, so the percentage becomes (50/(200+50)) = 0.2.

So the final dataframe that I want looks like below.

data2_perc =

Variables   A_1_perc      B_1_perc
Bins                                    
1           0.2           0.5
2           0.5           0.5
3           0.33          0.1428
4           0.5           0.5
5           0.4           0.5

How to do this?

Upvotes: 0

Views: 463

Answers (3)

jezrael
jezrael

Reputation: 862601

You can rename column of data1, sum, divide and last add DataFrame.add_suffix:

d = dict(zip(data1.columns,data2.columns))
df = (data2 / (data1.rename(columns=d) + data2)).add_suffix('_perc')
print (df)
           A_1_perc  B_1_perc
Variables                    
1          0.200000  0.500000
2          0.500000  0.500000
3          0.333333  0.142857
4          0.500000  0.500000
5          0.400000  0.500000

Upvotes: 1

M_S_N
M_S_N

Reputation: 2810

IIUC,

Make an empty dataframe

data2_perc=pd.DataFrame()

Then use following code to get desired output

data2_perc['A_1_perc']=data2['A_1']/(data2['A_1']+data1['A_0'])
data2_perc['B_1_perc']=data2['B_1']/(data2['B_1']+data1['B_0'])

Upvotes: 1

not_speshal
not_speshal

Reputation: 23146

data2_perc = pd.DataFrame()
data2_perc['A_1_perc'] = data2['A_1']/(data2['A_1']+data1['A_0'])
data2_perc['B_1_perc'] = data2['B_1']/(data2['B_1']+data1['B_0'])

Result:

    A_1_perc    B_1_perc
1   0.200000    0.500000
2   0.500000    0.500000
3   0.333333    0.142857
4   0.500000    0.500000
5   0.400000    0.500000

Upvotes: 3

Related Questions