2017-07-03 56 views
1

我有这样如何相互

DF1: Col1 Col2 
    Row1 A 30 
    Row2 B 40 

    DF2: Col1 Col2 
    Row1 A 10 
    Row2 B 5 

现在我想乘DF1.Col2和DF2.Col2并得到这样

输出2帧的数据乘以2只大熊猫的数据帧2列
Out: Col1 COl3 
    Row1 A 300 
    Row2 B 200 

我该怎么做。我试着波纹管代码,但它不工作

DF1[:,'Col3'] = pd.Series(DF1.Col2.astype(int)*DF2.Col2.astype(int),index=DF1.index) 

这直接与波纹管错误: 类型错误:unhashable型

+0

尝试DF1 ['COL3] = DF1.Col2.astype(int)* DF2.Col2.astype(int) –

回答

2

我想你需要:

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 
+0

是它的工作,非常感谢! – GihanDB

+0

很高兴能帮到你!数据字符串是否必须转换为整数? – jezrael

+0

是的,那些是字符串。 – GihanDB