我正在尝试使用python pandas数据框将多个列求和成一个新的sum列

时间:2018-10-22 02:44:09

标签: python pandas

我正在尝试学习python,并一直试图弄清楚如何创建我的数据的sum列。我想总结所有其他列。我创建新列,但所有总和值为零。可以找到数据here。我的代码如下,谢谢您的帮助:

import pandas as pd
#Importing csv file to chinaimport_df datafram
filename=r'C:\Users\Ing PC\Documents\Intro to Data Analysis\Final Project\CHINA_DOLLAR_IMPORTS.csv'
chinaimport_df = pd.read_csv(filename)

# Removing all rows that contain only zeros, thresh since since first column is words
chinaimport_df = chinaimport_df.dropna(how='all',axis=0, thresh=2) 

#Convert NANs to zeros
chinaimport_df=chinaimport_df.fillna(0)

#create a list of columns excluding the first column, to make sum func work later

col_list= list(chinaimport_df)
col_list.remove('Commodity')
print(col_list)

#adding column that sums 

chinaimport_df['Total'] = chinaimport_df[col_list].sum(axis=1)




chinaimport_df.to_csv("output.csv", index=False)

1 个答案:

答案 0 :(得分:2)

IIUC应该这样做。

import pandas as pd

df = pd.read_csv('CHINA_DOLLAR_IMPORTS.csv')

df['Total'] = df.replace(r',',"", regex=True).iloc[:, 1:].astype(float).sum(axis=1)

df.to_csv('output.csv', index=False)