0

I am having a list of pandas dataframes. I would like to filter out columns at each dataframe in for loop.

import pandas as pd
df_list=[]
sales = [('Jones LLC', 150, 200, 50),
        ('Alpha Co', 200, 210, 90),
        ('Blue Inc', 140, 215, 95)]
labels = ['account', 'Jan', 'Feb', 'Mar']

df_list.append(pd.DataFrame.from_records(sales, columns=labels))
sales = [('Jones LLC', 122, 566, 345),
        ('Alpha Co', 200, 210, 652),
        ('Blue Inc', 140, 215, 788)]
labels = ['account', 'Jan', 'Feb', 'Mar']
df_list.append(pd.DataFrame.from_records(sales, columns=labels))

col_list = [col for col in df_list[0].columns if col.startswith('acc') or col.startswith('mar')]

#this works
df_list[0]=df_list[0][col_list]
print(df_list[0])

#this does not work
for frame in df_list:
    frame = frame[col_list]
print(df_list[1])

Why doesn't for loop work?

1 Answer 1

1

Because your loop doesn't actually re-assign anything in df_list, it just iterates through. You can do:

for i, frame in enumerate(df_list):
    df_list[i] = frame[col_list]

Which works:

>>> df_list[0]
     account
0  Jones LLC
1   Alpha Co
2   Blue Inc
>>> df_list[1]
     account
0  Jones LLC
1   Alpha Co
2   Blue Inc
Sign up to request clarification or add additional context in comments.

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.