简体   繁体   中英

Pandas Dataframe Keep Row If Column Contains Any Designated Partial String

I have a pandas data frame. Below is a sample table.

Event   Text
A       something/AWAIT TO SHIP hello          
B       13579
C       AWAITING SHIP
D       24613
E       nan 

I want to only keep rows that contain the words "AWAIT TO SHIP" in the Text column or contains the string 13579 or 24613 in Text column. Below is my desired table:

Event   Text
A       something/AWAIT TO SHIP hello          
B       13579
D       24613

Below is the code I tried:

df_STH001_2 = df_STH001[df_STH001['Text'].str.contains("AWAIT TO SHIP") == True | df_STH001['Text'].str.contains("13579") == True | df_STH001['Text'].str.contains("24613") == True]

Below is the error I get:

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

You should not explicitly check == True , instead just use the call to contains .

Here's your sample:

First, we define the sample dataframe:

df1 = pd.DataFrame(data=[
('A', 'something/AWAIT TO SHIP hello'),
('B', 13579),
('C', 'AWAITING SHIP'),
('D', 24613),
('E', np.nan)], columns=['Event', 'Text'])

Then I build an intermediate mask with your conditions:

In [18]: mask = df1.Text.str.contains('AWAIT TO SHIP') |    \
                df1.Text.str.contains('13579') | \
                df1.Text.str.contains('24613')

Now you can index the original dataframe using this mask.

In [19]: df1.loc[mask]
Out[19]: 
  Event                           Text
0     A  something/AWAIT TO SHIP hello

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