Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/324.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_Data Science - Fatal编程技术网

Python 如何将两个数据帧中的两列相乘

Python 如何将两个数据帧中的两列相乘,python,pandas,data-science,Python,Pandas,Data Science,我有两个这样的数据帧 DF1: Col1 Col2 Row1 A 30 Row2 B 40 DF2: Col1 Col2 Row1 A 10 Row2 B 5 现在我想将DF1.Col2和DF2.Col2相乘,得到如下输出 Out: Col1 COl3 Row1 A 300 Row2 B 200 我怎样才能做到这一点。我尝试下面的代码,但它不工作

我有两个这样的数据帧

    DF1:    Col1 Col2
    Row1    A   30
    Row2    B   40

    DF2:  Col1 Col2
    Row1    A   10
    Row2    B   5
现在我想将DF1.Col2和DF2.Col2相乘,得到如下输出

    Out:   Col1 COl3
    Row1    A   300
    Row2    B   200
我怎样才能做到这一点。我尝试下面的代码,但它不工作

DF1[:,'Col3'] = pd.Series(DF1.Col2.astype(int)*DF2.Col2.astype(int),index=DF1.index)
这直接导致以下错误: 类型错误:不可损坏的类型

我认为您需要:

DF1['Col3'] = DF1.Col2.astype(int)*DF2.Col2.astype(int)
print (DF1)
     Col1  Col2  Col3
Row1    A    30   300
Row2    B    40   200
另一个解决方案包括:


试试DF1['COL3]=DF1.Col2.astype(int)*DF2.Col2.astype(int)乐意帮忙!是数据字符串,因此需要转换为整数吗?是的,这些是字符串。
DF1['Col3'] = DF1.Col2.astype(int).mul(DF2.Col2.astype(int))
print (DF1)
     Col1  Col2  Col3
Row1    A    30   300
Row2    B    40   200