0

I have a data-frame and one of its columns are a string which separated with dash. I want to get the part before the dash. Could you help me with that?

import pandas as pd 
df = pd.DataFrame()
df['a'] = [1, 2, 3, 4, 5]
df['b'] = ['C-C02','R-C05','R-C01','C-C06', 'RC-C06']

The desire output is: enter image description here

1
  • 2
    shouldn't row 4 have RC? Commented Jun 13, 2022 at 22:52

2 Answers 2

1

You could use str.replace to remove the - and all characters after it:

df['b'] = df['b'].str.replace(r'-.*$', '', regex=True)

Output:

   a   b
0  1   C
1  2   R
2  3   R
3  4   C
4  5  RC
Sign up to request clarification or add additional context in comments.

Comments

1

You want to split each string on the '-' character and keep the part before it:

df['c'] = [s.split('-')[0] for s in df['b']]

Comments

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.