Gevorg Nersesian
Gevorg Nersesian

Reputation: 25

Pandas DataFrame: the cells are modified but the changes do not save

I am a beginner in Data Science, so please sorry if my mistake is dumb.

Here, I have a loop which views my data frame and makes changes using .loc The problem is that the changes are not saved at the end. I checked every step, everything is processing right. I even checked the modified cell right after working on it (look below) and its gives the value I put into it. However, when the program finishes the my excel data frame is not changed at all.

Help please. Thank you in advance!

exc = pd.read_excel('...')

...

for x, y in exc.iterrows():
    y = y.copy() # got this row from some guy's code

    # ... a lot of conditions ...

    if check_num == check_opt:            
        exc.loc[x, 'PP'] = int(df['Docs'][every])

        print(exc.loc[x, 'PP']) # prints the new value -> int(df['Docs'][every])

Upvotes: 0

Views: 1238

Answers (1)

Arne
Arne

Reputation: 10545

when the program finishes my excel data frame is not changed at all.

That's because you never wrote anything to the Excel file. With exc = pd.read_excel('...') you create a Python object exc (more specifically, a pandas DataFrame), and all the subsequent modifications happen to this object. To change the source file accordingly, you can use pandas' DataFrame.to_excel() method, by adding this line in the end:

exc.to_excel("output.xlsx")

Instead of output, you can of course choose any file name you like (e.g. the name of your source file). Just be careful and keep in mind that this will overwrite any existing file by that name in your working directory.

Upvotes: 1

Related Questions