0

I have a list of dataframes, called list_models.

list_models=[df_gl,df_reg, df_in]

Each df consists of the same columns: Title, URL, Status

I am looking for a loop which looks for a string in Status and updates list_models.

list_models=[df[df['Status'].str.contains('In-depth', na=False)] |
         [df['Status'].str.contains('Approve', na=False)] 
         for df in list_models]

This code won't update the dfs in list_models and I don't understand why. Can anyone explain it to me?

Apparently someone already posted a similar question. However the answer is not really comprehensive, at least for me. Apply a for loop to multiple DataFrames in Pandas

chajamalka
  • 71
  • 1
  • 8

2 Answers2

0

You are doing df | [df] in your for-loop. pat argument of pandas.Series.str.contains() can detect multiple string at one time like

list_models = [df[df['Status'].str.contains('In-depth|Approve', na=False)]
               for df in list_models]
Ynjxsjmh
  • 28,441
  • 6
  • 34
  • 52
  • Hi, thanks for your suggestion. Unfortunately the problem explained above remains. The code will not update the dfs in my list. – chajamalka Apr 30 '21 at 05:14
  • @chajamalka What's the result of `df['Status'].str.contains('In-depth|Approve', na=False)` of each dataframe in list? – Ynjxsjmh Apr 30 '21 at 05:22
  • The loop should filter out those rows containing the words 'In-depth' and 'Approve' for each df. However, after looping through the list I can see that the filter was not applied. – chajamalka Apr 30 '21 at 05:59
  • @chajamalka If you want to filter out. Use `df[~df['Status'].str.contains('In-depth|Approve', na=False)]`. – Ynjxsjmh Apr 30 '21 at 06:03
  • @chajamalka I ask the output of `df['Status'].str.contains('In-depth|Approve', na=False)` is to ensure if this statement runs correctly. If it is all false, then this means your `Status` column doesn't contain `In-depth` or `Approve`. – Ynjxsjmh Apr 30 '21 at 06:38
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/231770/discussion-between-chajamalka-and-ynjxsjmh). – chajamalka Apr 30 '21 at 06:55
  • @chajamalka *This person has exactly the same problem.* It is obvious for me why that person faces the problem. Now I couldn't produce your problem. I ask the output of `df['Status'].str.contains('In-depth|Approve', na=False)` for more info for me to find the problem. – Ynjxsjmh Apr 30 '21 at 08:32
0

The loop above stores copies of the dataframes in the list, but does not alter them as I intended to.

Unpacking the list does the trick.

df_gl,df_reg, df_in=list_models
chajamalka
  • 71
  • 1
  • 8