1

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'

Ken Uda
  • 45
  • 2
  • 4
    df[df.str.contains('Morning')] – Kevin Choi May 07 '21 at 10:20
  • Does this answer your question? [How to filter rows containing a string pattern from a Pandas dataframe](https://stackoverflow.com/questions/27975069/how-to-filter-rows-containing-a-string-pattern-from-a-pandas-dataframe) – azro May 07 '21 at 10:23
  • It returns to this. AttributeError: 'DataFrame' object has no attribute 'str'. i checked dtypes it returns objects – Ken Uda May 07 '21 at 11:40

2 Answers2

0

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.

0

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

df = df.loc[df.astype(str).apply(' '.join,1).str.contains('Morning')]
Nk03
  • 14,699
  • 2
  • 8
  • 22