Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 990
alphanumeric Avatar asked Sep 02 '16 19:09

alphanumeric


People also ask

How do you check if a cell contains a string in pandas?

Using “contains” to Find a Substring in a Pandas DataFrame The contains method in Pandas allows you to search a column for a specific substring. The contains method returns boolean values for the Series with True for if the original Series value contains the substring and False if not.

How do you check if a value exists in a data frame?

You can check if a column contains/exists a particular value (string/int), list of multiple values in pandas DataFrame by using pd. series() , in operator, pandas. series. isin() , str.

How do you check special characters in pandas?

Pandas str. isalpha() method is used to check if all characters in each string in series are alphabetic(a-z/A-Z). Whitespace or any other character occurrence in the string would return false, but if there is a complete numeric value, then it would return NaN.


1 Answers

Use str and contains:

In [5]: df['a'].str.contains('-')
Out[5]: 
0    True
1    True
2    True
Name: a, dtype: bool
like image 71
juanpa.arrivillaga Avatar answered Sep 30 '22 16:09

juanpa.arrivillaga