Tshering Tashi
Tshering Tashi

Reputation: 27

I want to filter column with a character

I want to filter the column NAME with just one letter "o".
DataFrame:

NAME HOBBY
John football
Kelly chess

df["NAME"].filter(like ="o")

I am looking for an output:

NAME HOBBY
John football

Upvotes: 0

Views: 776

Answers (1)

Emi OB
Emi OB

Reputation: 3300

You can use .contains to filter only the rows where name contains a lower case 'o'.

Using the below df:

df = pd.DataFrame({'NAME' : ['John', 'Kelly'],
                   'HOBBY' : ['football', 'chess']})

Then you can use .loc and .contains to filter on desired rows:

  df.loc[df['NAME'].str.contains('o')]

Output:

   NAME     HOBBY
0  John  football

Upvotes: 1

Related Questions