Mayank Jain
Mayank Jain

Reputation: 45

Custom Filter on values in a dataframe column (Python)

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

Answers (2)

jezrael
jezrael

Reputation: 863351

I prefer here Series.str.split with str[-1]:

df["col1"] = df["col1"].str.str.split("-").str[-1]

Upvotes: 1

Sociopath
Sociopath

Reputation: 13426

You need:

df["col1"] = df["col1"].apply(lambda x: x.split("-")[-1])

Replace col1 with your column name

Upvotes: 1

Related Questions