简体   繁体   中英

Is there a way to multiply 'pandas' DataFrame based on the same columns or index?

For example, I have two table:

pA_B_array=np.array([[0.9,0.8,0.3],[0.1,0.2,0.7]])
pA_B=pd.DataFrame(pA_B_array,index=['A=F','A=T'],columns=['B=n','B=m','B=s']).stack()
pB_array=np.array([[0.97],[0.01],[0.02]])
pB = pd.DataFrame(pB_array,index=['B=n','B=m','B=s'])
A=F  B=n    0.9
     B=m    0.8
     B=s    0.3
A=T  B=n    0.1
     B=m    0.2
     B=s    0.7
dtype: float64
        0
B=n  0.97
B=m  0.01
B=s  0.02

I would multiply them based on the same labels or same indexes and get:

A=F  B=n    0.9*0.97
     B=m    0.8*0.01
     B=s    0.3*0.02
A=T  B=n    0.1*0.97
     B=m    0.2*0.01
     B=s    0.7*0.02

Is there any elegant way?

You can call .mul on pB and pass in pA_B and pass params level=1 to match on that index level and axis=0 so it's performed row-wise:

In [255]:

pB.mul(pA_B, level=1, axis=0)
Out[255]:
             0
A=F B=n  0.873
    B=m  0.008
    B=s  0.006
A=T B=n  0.097
    B=m  0.002
    B=s  0.014

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM