0

I have list of data frames, a list of 279 elements and each element is made of 10 data frames. So for example List_DATA[0][9] gives me the 9th data frame of the first list. I am trying to create a different list of data "List_Selected" in a loop using the values of "List_DATA":

List_Selected=[]
for i in range(0,279):
    List_Selected_temp=[]
    for j in range(0,10):
        List_Selected_temp.append(List_DATA[i][j][(List_DATA[i][j]['A']>3) & (List_DATA[i][j]['A']<5)])
        List_Selected.append(List_Selected_temp)

The problem is in the end I get identical data-frames values for all the 279 lists. So I have a bug somewhere and I cannot find it.I guess the lists are being overwritten, can anyone maybe spot the mistake in my code?

2
  • 1
    I think (I could be wrong, of course) you want to remove one indentation of List_Selected.append(List_Selected_temp) so that it is appending in for i instead of inside for j Commented Sep 2, 2021 at 11:46
  • @Karina, Thank you for your comment, you're right. Could you please write it as an answer? Commented Sep 2, 2021 at 11:57

3 Answers 3

1

Try this

List_Selected=[]
for i in range(0,279):
    List_Selected_temp=[]
    for j in range(0,10):
        df = List_DATA[i][j]
        List_Selected_temp.append(df.loc[(df['A']>3) & (df['A']<5)]
    List_Selected.append(List_Selected_temp)
Sign up to request clarification or add additional context in comments.

3 Comments

@the phoenix let me know if my answer helps you solve your problem.
@naturoArea51, thank you for your answer and help, unfortunately it did not work. The mistake was found.
@thephoenix that's ok.
0

A nice upgrade in terms of runtime that uses Python's multiprocess: Using concurrent module

import concurrent

def paralel_cond_func(curr_list_data):
    List_Selected_temp=[]
    for j in range(0,10):
        List_Selected_temp.append(curr_list_data[j][(curr_list_data[j]['A']>3) & (curr_list_data[j]['A']<5)])
    return List_Selected_temp

# number of processes
executor = concurrent.futures.ProcessPoolExecutor(10)

futures = [executor.submit(paralel_cond_func, group)
           for group in List_DATA]

concurrent.futures.wait(futures)
List_Selected = [res.result() for res in futures]

Comments

0

List_Selected.append(...) should be outside of the for j loop, but inside of the for i loop.

List_Selected=[]
for i in range(0,279):
    List_Selected_temp=[]
    for j in range(0,10):
        List_Selected_temp.append(List_DATA[i][j][(List_DATA[i][j]['A']>3) & (List_DATA[i][j]['A']<5)])
    List_Selected.append(List_Selected_temp)

Having it inside the for j loop making it appending 279*10 times, which is most probably unwanted behavior.

Comments

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.