如何将 2 pandas 数据框中的 2 列相互相乘
How to multiply 2 columns in 2 pandas data frames by each other
我有 2 个这样的数据框
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
另一个解决方案mul
:
DF1['Col3'] = DF1.Col2.astype(int).mul(DF2.Col2.astype(int))
print (DF1)
Col1 Col2 Col3
Row1 A 30 300
Row2 B 40 200
我有 2 个这样的数据框
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
另一个解决方案mul
:
DF1['Col3'] = DF1.Col2.astype(int).mul(DF2.Col2.astype(int))
print (DF1)
Col1 Col2 Col3
Row1 A 30 300
Row2 B 40 200