I am trying to sum two columns on my excel sheet. when I print df.[total] the values are correct. but it does not write a new column on the excel spreadsheet. how can I go about doing this?
Here is my excel sheet consists of :
Jan |Feb
10000 |62000
95000 |45000
91000 |120000
45000 |120000
162000 |120000
Goal: to sum the two values and write a new sum column of the resulting sums. and get on my excel spreadsheet:
Jan |Feb |Sums
10000 |62000| 72000
95000 |45000|140000
91000 |120000 |211000
45000 |120000 | 165000
162000 |120000 | 282000
Here is my code:
import pandas as pd
import numpy as np
from pandas import ExcelWriter
df = pd.read_excel("samplesheet.xlsx")
df["total"] = df["Jan"] + df["Feb"]
df.head()
#print(df["total"])
df_total = df["total"]
print(df_total)
df_total = pd.DataFrame(df_total)
writer = ExcelWriter('samplesheet.xlsx')
df_total.to_excel(writer,'Sheet1')
writer.save()
After printing df["total"] it results in: 72000, 140000, 211000, 165000, 282000. As you can see the summing is actually working but i am not sure write a new column to the excel sheet with the column name "sums" with the pertaining sums for the specific rows.
Thanks