-3
df.loc[df['name'] == 'Mary']

The above get rows if the 'name' is Mary. What about if I want rows that contains 'Mary', not exactly equal 'Mary'?

marlon
  • 6,029
  • 8
  • 42
  • 76

1 Answers1

1

You can use pd.Series.str.contains() method to achieve this.

df[df['name'].str.contains('Mary')]
Tobias P. G.
  • 827
  • 8
  • 15
  • Is '|' is a special character that doesn't work with the contains function? I used df[df['name'].str.contains('|')] and it selected all rows. – marlon Nov 05 '21 at 19:58
  • 1
    try to escape the symbol by writing df[df['name'].str.contains('\|')] –  Nov 05 '21 at 20:04