Home > Net >  split the string in dataframe in python
split the string in dataframe in python

Time:06-14

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

CodePudding user response:

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']]

CodePudding user response:

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
  • Related