0

I need to store 10 data frames into a list. The data frames are the outputs of a function. The problem is that the list remains all the same as the last data frame. For example, resultlist[0] suppose to be the data frame of 01-Feb-2020. But every data frame in the list is the output from 10-Feb-2020.

resultList = [None]*10

for i in range(0,10):    
    resultList[i]= getResultfunction(id,listDates[i])
0

1 Answer 1

1

As far as I can see, the code snippet you provided is correct. Just know that you are giving the same id parameter to each getResultfunction call. Could you show the implementation of the getResultfunction? The problem is probably in there. Either that or your listDates list contains the wrong dates.

EDIT 1 Alright so the problem is probably that you are just storing references to the same dataframe in the list. If you are not familiar with how programming languages store references and values, I'd suggest that you check out this link https://blog.penjee.com/passing-by-value-vs-by-reference-java-graphical/ or just google it, as its a fundamental paradigm of programming languages.

So basically, each time you call getResultfunction, you get a reference to the same template, then you alter this template, and return a reference to the template. Next time you call the function, you access the same (altered) template, change it again, and return a new reference to it. The list then stores lots of references to the same object!

In this particular case, each call to getResultfunction should create a new object. I see 2 solutions; Each time you call getResultfunction you:

  • just copy the existing template, which generates a new identical object:
def getResultfunction(id, date):
    p = template.copy()
    """other computations"""
    return p
  • create a new template from scratch:
def getResultfunction(id, date):
    data = {...}
    df = pd.Dataframe(data)
    """"other computations"""
    return df
Sign up to request clarification or add additional context in comments.

5 Comments

HI, Emilio thank you for you answer. I just checked again. So I did it manually.. resultList[0]= getResultfunction(id,listDates[0]) resultList[1]= getResultfunction(id,listDates[1]) ... The last dataframe will overwrite every item in the list. Is there another way to store individual dataframe in a different data structure which will work?
It really depends on what type you are storing in the list. You are probably storing references to the same dataframe in the list and then just changing the same dataframe over and over again. Can you provide the code of the getResultfunction?
i am storing a panda df into the list. the function is similar to this: def getResultFucntion(id,date): p = template p.loc[0,1] = date p.loc[0,5] = id ... return p
I edited my post, I think this should solve your problem :)
hey emilio thank you for your help! The problem is solved!! : )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.