Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select rows in Pandas which does not contain a specific character

Tags:

python

pandas

I need something similar to

.str.startswith() 
.str.endswith()

but for the middle part of a string.

For example, given the following pd.DataFrame

      str_name
   0    aaabaa
   1    aabbcb
   2    baabba
   3    aacbba
   4    baccaa
   5    ababaa

I need to throw rows 1, 3 and 4 which contain (at least one) letter 'c'.
The position of the specific letter ('c') is not known.
The task is to remove all rows which do contain at least one specific letter

like image 292
Arnold Klein Avatar asked Jan 20 '17 00:01

Arnold Klein


People also ask

How do you select a row with specific text in Python?

Method 1 : Using contains() Using the contains() function of strings to filter the rows. We are filtering the rows based on the 'Credit-Rating' column of the dataframe by converting it to string followed by the contains method of string class.


2 Answers

You want df['string_column'].str.contains('c')

>>> df
  str_name
0   aaabaa
1   aabbcb
2   baabba
3   aacbba
4   baccaa
5   ababaa
>>> df['str_name'].str.contains('c')
0    False
1     True
2    False
3     True
4     True
5    False
Name: str_name, dtype: bool

Now, you can "delete" like this

>>> df = df[~df['str_name'].str.contains('c')]
>>> df
  str_name
0   aaabaa
2   baabba
5   ababaa
>>>

Edited to add:

If you only want to check the first k characters, you can slice. Suppose k=3:

>>> df.str_name.str.slice(0,3)
0    aaa
1    aab
2    baa
3    aac
4    bac
5    aba
Name: str_name, dtype: object
>>> df.str_name.str.slice(0,3).str.contains('c')
0    False
1    False
2    False
3     True
4     True
5    False
Name: str_name, dtype: bool

Note, Series.str.slice does not behave like a typical Python slice.

like image 170
juanpa.arrivillaga Avatar answered Sep 22 '22 14:09

juanpa.arrivillaga


you can use numpy

df[np.core.chararray.find(df.str_name.values.astype(str), 'c') < 0]

  str_name
0   aaabaa
2   baabba
5   ababaa
like image 39
piRSquared Avatar answered Sep 22 '22 14:09

piRSquared