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

Time:07-06

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?

CodePudding user response:

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
  • Related