-1

I am trying to take several numbers from a few different columns of a data frame and sum them and create a new row called "Total" directly below the last line. Below are some example data that I am working with.

import pandas as pd
option = pd.DataFrame(['Option1','Option2','Option3','Option4'])
Volume = pd.DataFrame([1234,23423,2331,23135])
dataframe1 = pd.concat([option, Volume],axis=1)

You will see that I am looking at a two-column dataframe with the "Options" column and then a numerical value. The end goal is to have a "Total" column at the end of the option 4 row this will need to be an aggregate of all the numbers in Options 1-4. Please let me know the best way to go about this! Thank you

wjandrea
  • 28,235
  • 9
  • 60
  • 81
ARE
  • 99
  • 9
  • 1
    There's a surprising amount to unpack here. Firstly, have you done a Pandas tutorial? Any of them should cover how to sum a column. Secondly, why on Earth are you creating a DF like this, in three steps instead of one, and without any labels? Doing it like that means they have the same label (`0`), which means you have to use `.iloc`. Third, where are the "different columns" you mentioned? There's only one here. If you already know how to do indexing and slicing, the difference is unimportant, but given that you don't know how to sum a column, I doubt you do. So, please do a Pandas tutorial. – wjandrea Aug 15 '22 at 16:43
  • Sorry if it looks a bit odd - I am working on a much larger dataset and came up with example data quickly to highlight my issue and end goal (As I am uploading multiple CSV files). – ARE Aug 15 '22 at 16:59

1 Answers1

1
totalcount = dataframe1.iloc[:,1].sum()
dataframe1.loc[len(dataframe1.index)] = ['Total', totalcount]  # This is for Row
dataframe1["Total"] = totalcount  # This is for Column
print(dataframe1)

Screenshot showing column output and row output (50123 for all values)

wjandrea
  • 28,235
  • 9
  • 60
  • 81
Unkown
  • 67
  • 3
  • I'm pretty sure OP misspoke when they said they wanted a "Total column". It seems redundant anyway. – wjandrea Aug 15 '22 at 16:52
  • Thank you for the help here - dataframe1.loc[len(dataframe1.index)] = ['Total', totalcount] helped a ton! – ARE Aug 15 '22 at 16:58