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.

runawaykid
  • 1,391
  • 1
  • 15
  • 20

1 Answers1

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']
Mohamed Ali JAMAOUI
  • 14,275
  • 14
  • 73
  • 117