Reputation: 57
I have a list of dataframes:
list = [a, b, c, d, e ,f]
Each of these dataframes has a column called 'PEOPLE', and I want to loop through this list and drop any rows in any of the data frames where the value in 'PEOPLE' is one of the values in another list:
people_to_drop = ['PERSON ABC','PERSON DEF']
I've been trying the following method:
for index,item in enumerate(list):
list[index] = item[~item['PEOPLE'].isin(people_to_drop)].copy()
However, when I check the datasets after the for loop has run, they don't seem to have dropped the rows I want dropped. Is there a better way to do this? Thanks in advance!
Upvotes: 1
Views: 474
Reputation: 25259
You may try list comprehension as follows
df_list = [a, b, c, d, e ,f]
new_df_list = [a_df[~a_df['PEOPLE'].isin(people_to_drop)].copy() for a_df in df_list]
After that, print out each dataframe in new_df_list
to check
print(new_df_list[0])
print(new_df_list[1])
Upvotes: 3