Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (17.6k points)

Assume we have a data frame in Python Pandas that looks like this:

df = pd.DataFrame({'vals': [1, 2, 3, 4], 'ids': [u'aball', u'bball', u'cnut', u'fball']})

Or, in table form:

ids    vals

aball   1

bball   2

cnut    3

fball   4

How do I filter rows which contain the key word "ball?" For example, the output should be:

ids    vals

aball   1

bball   2

fball   4

1 Answer

0 votes
by (41.4k points)

Use the below snippet:

In [3]: df[df['ids'].str.contains("ball")]

Out[3]:

     ids  vals

aball     1

bball     2

fball     4

If you want to learn more about Pandas then visit this Python Course designed By the industrial Experts.

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...