2017-07-03 65 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