我想总结3列的值,例如3 4 10
我想要结果17
不是3410
! 我做了以下内容:如何总结3列熊猫python
df2["OverAll"] = df2['reading'] + df2['math'] + df2['writing']
但它给我的3410(把3列在一起!)。
请帮忙!
我想总结3列的值,例如3 4 10
我想要结果17
不是3410
! 我做了以下内容:如何总结3列熊猫python
df2["OverAll"] = df2['reading'] + df2['math'] + df2['writing']
但它给我的3410(把3列在一起!)。
请帮忙!
那是因为那些列是字符串。
尝试:
df['OverAll'] = df[['reading', 'math', 'writing']].astype(float).sum(1)
也试过这个,它的工作,谢谢! – Rachel
你需要你的第一列转换为数字dtypes:
df[['reading','math','writing']] = \
df[['reading','math','writing']].apply(pd.to_numeric, errors='coerce')
看来你colums包括字符串,而不是数字。显示生成列的代码。 –