'How to save my first dataframe value with Pandas?

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



Solution 1:[1]

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.

Solution 2:[2]

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()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Payam
Solution 2 sandor