2017-05-03 120 views
2

我想总结3列的值,例如3 4 10我想要结果17不是3410! 我做了以下内容:如何总结3列熊猫python

df2["OverAll"] = df2['reading'] + df2['math'] + df2['writing'] 

但它给我的3410(把3列在一起!)。

请帮忙!

+2

看来你colums包括字符串,而不是数字。显示生成列的代码。 –

回答

2

那是因为那些列是字符串。

尝试:

df['OverAll'] = df[['reading', 'math', 'writing']].astype(float).sum(1) 
+0

也试过这个,它的工作,谢谢! – Rachel

2

你需要你的第一列转换为数字dtypes:

df[['reading','math','writing']] = \ 
    df[['reading','math','writing']].apply(pd.to_numeric, errors='coerce')