Skip to content
Advertisement

Remove characters from column

I am trying to remove “0” and “:” from a column in a dataframe. The code I use is,

df_Main["Call Length"].str.replace(":", "")
df_Main["Call Length"].str.replace("0", "")
df_Main

Output:

enter image description here

The result does not remove “0” and “:” How can I go about this?

Advertisement

Answer

You’re missing to assignment of the replacement back to the original column:

df_Main["Call Length"] = df_Main["Call Length"].str.replace(":", "")
df_Main["Call Length"] = df_Main["Call Length"].str.replace("0", "")
df_Main

Though you can carry out this operation with only one application of replace:

df_Main["Call Length"] = df["Call Length"].str.replace("[:0]+", "", regex=True)
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement