Issue
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?
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
Answered By - Sheldon
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.