Yahia A. Seridi
Yahia A. Seridi

Reputation: 33

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?

Upvotes: 0

Views: 37

Answers (1)

Sheldon
Sheldon

Reputation: 4653

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

Upvotes: 1

Related Questions