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

I am trying to remove a few characters from columns in a dataframe

So I have two dataframes, they’re supposed to have a column in common, but one of the dataframes has a few characters preventing me from merging them, (DSO).

is there a way to remove an exact string and special characters from a column?

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

>Solution :

You can simply use the pandas replace method to remove the unwanted string from your pandas Series.

For instance:

import pandas as pd
df = pd.DataFrame({"Character": ["Kenny", "Cartman", "Eric", "Stan"]})
df["Character"].str.replace('Kenny','')

EDIT: In your example, I had to use regex=False to get rid of the parentheses.

import pandas as pd
df = pd.DataFrame({"Character": ["(DSO)", "Cartman", "Eric", "Stan"]})
df["Character"].str.replace("(DSO)",'', regex=False)

This snippet returned:

0           
1    Cartman
2       Eric
3       Stan
Name: Character, dtype: object
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