Home > Enterprise >  How to save my first dataframe value with Pandas?
How to save my first dataframe value with Pandas?

Time:05-07

I just don't get it. I'm trying to save two different value(to different position) to an excel file, but the first one gets overwritten everytime. Why?

@classmethod
def openexcel(cls):
    months = {"Január", "Február", "Március"}
    df = pd.DataFrame(months, columns=["Months"])
    df.to_excel("budget.xlsx", "a ", index=False)
    
    months2 = {"Jan", "Feb", "March"}
    x = pd.DataFrame(months2, columns=["Months2"])
    x.to_excel("budget.xlsx", "a ", index=False, startcol=5, startrow=2)

enter image description here

CodePudding user response:

This is because pandas doesn't know about the old status of your excel file,you need to read old file first and build a new datafame from your old status, and add new data to and finally save it. If you want to a more granular way of savings data, you possibly need a database like sql based ones.

CodePudding user response:

So, I figured out if anyone in the same case like me.

@classmethod
def months(cls):
    writer = pd.ExcelWriter('./income.xlsx', engine='xlsxwriter')
    months = month_cb["values"]
    df = pd.DataFrame(months, columns=["Months"])
    df.to_excel(writer, index=False)
    months1 = title_cb["values"]
    df1 = pd.DataFrame(months1, columns=["Months"])
    df1.to_excel(writer, startcol=1, startrow=1, index=False)
    writer.save()
  • Related