Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将两个单独的数据帧相乘_Python_Pandas_Dataframe - Fatal编程技术网

Python 将两个单独的数据帧相乘

Python 将两个单独的数据帧相乘,python,pandas,dataframe,Python,Pandas,Dataframe,我有两个数据帧,我想通过应用一个简单的函数来使用它们创建第三个数据帧。数据集非常大,因此,有没有更有效的方法来代替循环遍历每一行和每一列 # dfA id | value | mars | 10 | jupt | 15 | satn | 14 | # dfB id | A | B | C | satn | 0.5 | 0.3 | 0.2 | mars | 0.2 | 0.5 | 0.3 | jupt | 0.6 | 0.3 | 0.1 | 最终结果应该

我有两个数据帧,我想通过应用一个简单的函数来使用它们创建第三个数据帧。数据集非常大,因此,有没有更有效的方法来代替循环遍历每一行和每一列

# dfA
id   | value |
mars | 10    |
jupt | 15    |
satn | 14    |


# dfB
id   | A   | B   | C   |
satn | 0.5 | 0.3 | 0.2 |
mars | 0.2 | 0.5 | 0.3 |
jupt | 0.6 | 0.3 | 0.1 |
最终结果应该是
dfA
乘以
dfB
中相应的
id

# dfResult
id   | A      | B      | C      |
mars | 10*0.2 | 10*0.5 | 10*0.3 |
jupt | 15*0.5 | 15*0.3 | 15*0.1 |
satn | 14*0.5 | 14*0.3 | 14*0.2 |
用于从
索引
中匹配、按多个和最后创建列:

df = dfB.set_index('id').mul(dfA.set_index('id')['value'], axis=0).reset_index()
print (df)
     id    A    B    C
0  jupt  9.0  4.5  1.5
1  mars  2.0  5.0  3.0
2  satn  7.0  4.2  2.8