2

I have N dataframes ranging from L1...Ln.

I would like to modify them to keep rows pertaining to a certain condition.

I ran the following loop:

for df in [L1,...,Ln]:
    df=df.ix[df['Sector']=='Services']

However, when I call out each dataframe, I find it has not been replaced accordingly. How do I modify a set of dataframes using a loop?

I am using Python 2.7.

0

1 Answer 1

2

You need to overwrite the old dataframe with the new one:

all_dfs = [L1,...,Ln]
# iterate through the dataframes one by one
# keep track of the order in index and the content in df 
for index, df in enumerate(all_dfs):
    # modify the current dataframe df 
    # then overwrite the old one in the same index. 
    all_dfs[index]= df.ix[df['Sector']=='Services']
Sign up to request clarification or add additional context in comments.

4 Comments

Am I not doing so currently through df=df...?
@runawaykid you are overwriting the local variable df. but not the actual df in the list.
I find that I need to add the line [L1,...Ln] = all_dfs at the end for this to work when I call out L1. If not its still the original dataframe.
@runawaykid of course, because all_dfs is a copy of [L1,...Ln]. In anycase, you have to assign your list [L1,...Ln] to a variable and use the variable instead of the list..

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.