0

I'm trying to export multiple dataframes from python to excel, I wrote a for loop to do so. Following an answer from another thread https://stackoverflow.com/a/54237619/12350323 . I think the structure of my code is the same as the one from that thread.

The problem is that I don't get the expected result when I run the code . What I should be getting (the way I intended the code to work) is 8 sheets, every loop generating two different dataframes, one with my function applied and another one with dropped values, "redummy" and "deldummy" and this done four times.

What I get instead is two exact copies of the dataframe with my function applied in every loop. That is, I get two "redummy"s, but they have a different sheet name, so I end up with 8 sheets, with only four that are different, so I think my code is going through the loop, but deldummy doesn't show up for some reason. I don't really understand why this is happening. I hope you can help me, thanks in advance.

import pandas as pd
from pandas import ExcelWriter
w = ExcelWriter(r'C:\Users\DATA\Downloads\data int.xlsx')

for i in range(4): 
    dummy = pd.read_excel(r'C:\Users\UNI\Downloads\DATA.xlsx', sheet_name = "M" + i)
    redummy = poly_fill(dummy, 4) #a function of my own, it shouldn't change "dummy"
    deldummy = dummy.dropna()
    redummy.to_excel(w, sheet_name = "L4 " + i + "%", index = False)
    deldummy.to_excel(w, sheet_name = "del " + i + "%", index = False)
w.save()
  • Do you mean `for i in range(4)` because what you have should lead to a TypeError – ALollz Sep 10 '20 at 16:20
  • yes I meant that, my code uses a vector of strings so it doesn't lead to errors, I just changed it for the question to be easier to understand – Irving Pérez Sep 10 '20 at 16:27

0 Answers0