使用pandas写一个特定的DF到下一个空列

我正在尝试编写一个与两个单元格计算的总值相关的特定df,然后用写在下一个空列上的值编写一个新的“total”列。

Excel工作表包括:

Jan |Feb 10000 |62000 95000 |45000 91000 |120000 45000 |120000 162000 |120000 

我想要的是:

 Jan |Feb |Total 10000 |62000| 72000 95000 |45000|140000 91000 |120000 |211000 45000 |120000 | 165000 162000 |120000 | 282000 

而不是像我想的那样将总计列写入下一列,它只是覆盖整个文件,只显示总计列。 我会如何去写我的df_totals到我想要的下一个空列?

码:

 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["Mar"] df.head() df_total = df["total"] print(df_total) print("") df_total = pd.DataFrame(df_total) writer = ExcelWriter('samplesheet.xlsx') df_total.to_excel(writer,'Sheet1',index=False) writer.save() 

运行代码后xlsx内的内容:

 Total 72000 140000 211000 165000 282000 

谢谢

df_total是一个系列 – df

 df_total = df["total"] 

如果你想保存DataFrame,那么df

 df_total.to_excel(writer,'Sheet1',index=False) 

应该

 df.to_excel(writer,'Sheet1',index=False)