1

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?

2
  • are you using any library to read excel in python? Commented Dec 2, 2019 at 7:26
  • Yes. I am using Pandas Commented Dec 2, 2019 at 7:27

2 Answers 2

1

You need:

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

Replace col1 with your column name

Sign up to request clarification or add additional context in comments.

2 Comments

why not used str pandas functions?
@jezrael coz I couldn't come up with .str.str part
1

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

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

1 Comment

should be faster then apply

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.