4

I need to find special characters from entire dataframe.

In below data frame some columns contains special characters, how to find the which columns contains special characters?

enter image description here

Want to display text for each columns if it contains special characters.

Learnings
  • 2,780
  • 9
  • 35
  • 55
  • @Sqoshu OK, It would be great if you provide some code example? – Learnings Jul 11 '18 at 14:25
  • 1
    Please take time to read this post on [how to provide a great pandas example](http://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) as well as how to provide a [minimal, complete, and verifiable example](http://stackoverflow.com/help/mcve) and revise your question accordingly. These tips on [how to ask a good question](http://stackoverflow.com/help/how-to-ask) may also be useful. – Zero Jul 11 '18 at 14:35

3 Answers3

7

You can setup an alphabet of valid characters, for example

import string
alphabet = string.ascii_letters+string.punctuation

Which is

'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'

And just use

df.col.str.strip(alphabet).astype(bool).any()

For example,

df = pd.DataFrame({'col1':['abc', 'hello?'], 'col2': ['ÃÉG', 'Ç']})


    col1    col2
0   abc     ÃÉG
1   hello?  Ç

Then, with the above alphabet,

df.col1.str.strip(alphabet).astype(bool).any()
False
df.col2.str.strip(alphabet).astype(bool).any()
True

The statement special characters can be very tricky, because it depends on your interpretation. For example, you might or might not consider # to be a special character. Also, some languages (such as Portuguese) may have chars like ã and é but others (such as English) will not.

rafaelc
  • 57,686
  • 15
  • 58
  • 82
7

To remove unwanted characters from dataframe columns, use regex:

def strip_character(dataCol):
    r = re.compile(r'[^a-zA-Z !@#$%&*_+-=|\:";<>,./()[\]{}\']')
    return r.sub('', dataCol)

df[resultCol] = df[dataCol].apply(strip_character)
Plinus
  • 308
  • 1
  • 3
  • 10
1
# Whitespaces also could be considered in some cases.

import string

unwanted = string.ascii_letters + string.punctuation + string.whitespace
print(unwanted)

# This helped me extract '10' from '10+ years'.

df.col = df.col.str.strip(unwanted)
pzaenger
  • 11,381
  • 3
  • 45
  • 46