Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to drop pandas dataframe columns containing special characters

How do I drop pandas dataframe columns that contains special characters such as @ / ] [ } { – _ etc.?

For example I have the following dataframe (called df):

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I need to drop the columns Name and Matchkey becasue they contain some special characters.
Also, how can I specify a list of special characters based on which the columns will be dropped?

For example: I’d like to drop the columns that contain (in any record, in any cell) any of the following special characters:

listOfSpecialCharacters: ¬,`,!,",£,$,£,#,/,\

>Solution :

One option is to use a regex with str.contains and apply, then use boolean indexing to drop the columns:

import re
chars = '¬`!"£$£#/\\'
regex = f'[{"".join(map(re.escape, chars))}]'
# '[¬`!"£\\$£\\#/\\\\]'

df2 = df.loc[:, ~df.apply(lambda c: c.str.contains(regex).any())]

example:

# input
     A    B    C
0  123  12!  123
1  abc  abc  a¬b

# output
     A
0  123
1  abc
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading