简体   繁体   中英

Searching for a string in a list of pandas dataframes

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

You are doing df | [df] 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]

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM