Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/351.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 如何使用点积组合2个数据帧_Python_Python 3.x_Pandas_Numpy - Fatal编程技术网

Python 如何使用点积组合2个数据帧

Python 如何使用点积组合2个数据帧,python,python-3.x,pandas,numpy,Python,Python 3.x,Pandas,Numpy,我有两个数据帧: df_1 = pd.DataFrame({"c1":[2,3,5,0], "c2":[1,0,5,2], "c3":[8,1,5,1]}, index=[1,2,3,4]) df_2 = pd.DataFrame({"u1":[1,0,1,0], "u2":[-1,0,1,1]},

我有两个数据帧:

df_1 = pd.DataFrame({"c1":[2,3,5,0], 
                     "c2":[1,0,5,2], 
                     "c3":[8,1,5,1]}, 
                     index=[1,2,3,4])

df_2 = pd.DataFrame({"u1":[1,0,1,0], 
                     "u2":[-1,0,1,1]}, 
                     index=[1,2,3,4])
对于c和u的每一个组合,我想计算点积,例如,用np.dot

例如,c1-u1的值是这样计算的:2*1+3*0+5*1+0*0=7

生成的数据帧应如下所示:

     u1   u2
c1   7    3
c2   6    6
c3   13   -2
是否有一种优雅的方法来解决这个问题,或者迭代2个数据帧是唯一的方法?

您的意思是:

df_1.T @ df_2
# or equivalently
# df1.T.dot(df2)
输出:

    u1  u2
c1   7   3
c2   6   6
c3  13  -2
    u1  u2
c1  7   3
c2  6   6
c3  13  -2
我们可以使用熊猫点函数

df_1.T.dot(df_2)
输出:

    u1  u2
c1   7   3
c2   6   6
c3  13  -2
    u1  u2
c1  7   3
c2  6   6
c3  13  -2