I am trying to remove “0” and “:” from a column in a dataframe. The code I use is,
JavaScript
x
4
1
df_Main["Call Length"].str.replace(":", "")
2
df_Main["Call Length"].str.replace("0", "")
3
df_Main
4
Output:
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:
JavaScript
1
4
1
df_Main["Call Length"] = df_Main["Call Length"].str.replace(":", "")
2
df_Main["Call Length"] = df_Main["Call Length"].str.replace("0", "")
3
df_Main
4
Though you can carry out this operation with only one application of replace
:
JavaScript
1
2
1
df_Main["Call Length"] = df["Call Length"].str.replace("[:0]+", "", regex=True)
2