python求多个数的乘积相等_在python pandas中将两个具有相似索引的数据框相乘

I have two data frames and I want to multiply them by index. What the best way to do this?

NOTE: Column names are different.

df1 = pd.DataFrame([(1,2,3),(3,4,5),(5,6,7)], columns=['a','b','d'], index = ['A', 'B','C'])

df1

a b d

A 1 2 3

B 3 4 5

C 5 6 7

df2 = pd.DataFrame([(10,20,30)], columns=['A','B','C'],index = ['ss'])

df2 = df2.transpose()

df2

ss

A 10

B 20

C 30

Output Dataframe:

a b d

A 10 20 30

B 60 80 100

C 150 180 210

解决方案

Call mul and convert the Series to a list and pass axis=0, by converting to a list we ignore any alignment errors with the index/column names:

In [74]:

df1.mul(list(df2['ss']), axis=0)

Out[74]:

a b d

A 10 20 30

B 60 80 100

C 150 180 210

EDIT

No need to convert to a list just access the Series directly:

In [75]:

df1.mul(df2['ss'], axis=0)

Out[75]:

a b d

A 10 20 30

B 60 80 100

C 150 180 210

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值