0

I'm trying to learn python and have been trying to figure out how to create a sum column of my data. I want to sum all other columns. I create the new column but all sum values are zero. The data can be found here. My code is below, thank you for the help:

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)
2
  • Okay, what's desired output? Commented Oct 22, 2018 at 2:46
  • I'm trying to find the sum of the columns. Basically the total imports of each commodity for the year until August. Commented Oct 22, 2018 at 2:49

1 Answer 1

2

IIUC this should do it.

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)
Sign up to request clarification or add additional context in comments.

5 Comments

Thank,this could be done by using applymap but your code seems more concise
@BAKEZQ Yes we can but I think both replace and sum should be enough to do the work here. :)
Thanks a lot for the help Abhi! I guess part of the problem was the commas which you removed. But what are you doing with .iloc and .astype? Is that just for converting everything to a float? Wasn't the data already set as a float?
@user2606257 First of all iloc is slicing the dataframe by excluding the first column which is commodity in our case. Even though we removed the comma from the values the datatype of all the values are stillstrings so we have to convert them to integer for the sum function to work. Here astype converts all the columns to numeric float in this case.
Perfect, thanks a lot for the clarification, that makes a lot of sense.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.