2017-07-17 98 views
0

命令如何將熊貓數據框的兩列(行乘法)相乘並將結果存儲在新列中?

df_main['Ranking'] = df_main['Porosity']*['Permeability']給 -

TypeError: can't multiply sequence by non-int of type 'str'

的attacehd圖片(https://i.stack.imgur.com/0Asu3.png)提供了更多信息。我的代碼段是在i.stack.imgur.com/ehqF5.png)

更多信息:https://i.stack.imgur.com/0Asu3.png

+0

你嘗試'df_main [ '排名'] = df_main [ '孔隙率'] * df_main [ '滲透性']'? – SethMMorton

回答

0

首先,你需要兩列的列類型轉換爲浮動(否則你無法將它們相乘) 。你可以做如下:

df_main[['Porosity', 'Permeability']] = df_main[['Porosity', 'Permeability']].astype(float) 

然後你就可以定義通過乘法新列:

df_main['Ranking'] = df_main['Porosity']*df_main['Permeability'] 
+1

我不太確定'.type(float)'步驟是否需要。我認爲問題在於他們試圖將列乘以僅包含字符串的單元素列表。 – SethMMorton

+1

@SethMMorton在問題的imgur圖中產品的語法看起來是正確的。 –

+0

當我嘗試更正形式的米里亞姆的答案時,我得到以下錯誤值錯誤:無法將字符串轉換爲浮點數:'12 .863486 2' –

相關問題