alphanumeric
alphanumeric

Reputation: 19339

How to check if character exists in DataFrame cell

After creating the three-rows DataFrame:

import pandas as pd
df = pd.DataFrame({'a': ['1-2', '3-4', '5-6']})

I check if there is any cell equal to '3-4':

df['a']=='3-4'

enter image description here

Since df['a']=='3-4' command results to pandas.core.series.Series object I can use it to create a "filtered" version of the original DataFrame like so:

filtered = df[ df['a']=='3-4' ]

enter image description here

In Python I can check for the occurrence of the string character in another string using:

string_value = '3-4'
print('-' in string_value)

What would be a way to accomplish the same while working with DataFrames?

So, I could create the filtered version of the original DataFrame by checking if '-' character in every row's cell, like:

filtered = df['-' in df['a']]

But this syntax above is invalid and throws KeyError: False error message.

Upvotes: 9

Views: 29065

Answers (2)

Oren
Oren

Reputation: 5319

This is how to do it using query:

In [4]: df.query('a.str.contains("1")')
Out[4]: 
     a
0  1-2

In [5]: df.query('a.str.contains("-")')
Out[5]: 
     a
0  1-2
1  3-4
2  5-6

Upvotes: 0

juanpa.arrivillaga
juanpa.arrivillaga

Reputation: 96098

Use str and contains:

In [5]: df['a'].str.contains('-')
Out[5]: 
0    True
1    True
2    True
Name: a, dtype: bool

Upvotes: 22

Related Questions