Skip to content Skip to sidebar Skip to footer

How To Change Dataframes Using A Loop In Python Pandas?

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[

Solution 1:

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']

Post a Comment for "How To Change Dataframes Using A Loop In Python Pandas?"