简体   繁体   中英

Selecting rows based on value pandas

I am trying to select only the row that consist the value Morning

ID 16615 Morning, Morning, Night, Evening
ID 16617 Night, Night, Night, Night
ID 16618 Evening, Morning
ID 16619 Evening, Night, Morning, Morning
ID 16620 Evening, Evening, Evening, Afternoon
ID 166621 Afternoon, Afternoon, Afternoon, Morning

Output

ID 16615 Morning, Morning, Night, Evening
ID 16618 Evening, Morning
ID 16619 Evening, Night, Morning, Morning
ID 166621 Afternoon, Afternoon, Afternoon, Morning

Meaning to only select rows that consist 'Morning'

Try this:

df[df.str.contains('Morning', regex=False)] 

Meaning from your DataFrame (df) select only df's rows that contain the word 'Morning' without implement it in regex query.

If you need to check in all columns then you can try:

df = df.loc[df.astype(str).apply(' '.join,1).str.contains('Morning')]

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