Reputation: 45
I have a column which has values of 6 charaters, followed by "-", followed by a number. I want to replace the original value with the number only.
For example - If the column has a value of ABCXYZ-123. I need to replace it with 123. Need it for all the rows of that column.
I was also able to right an Excel formula for the same - =NUMBERVALUE(MID(B2,(FIND("-",B2)+1),LEN(B2)))
How to do the same in Python?
Upvotes: 1
Views: 50
Reputation: 863351
I prefer here Series.str.split
with str[-1]
:
df["col1"] = df["col1"].str.str.split("-").str[-1]
Upvotes: 1
Reputation: 13426
You need:
df["col1"] = df["col1"].apply(lambda x: x.split("-")[-1])
Replace col1
with your column name
Upvotes: 1