Skip to content
Advertisement

split the string in dataframe in python

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

Advertisement

Answer

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
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement