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?
JavaScript
x
5
1
import pandas as pd
2
df = pd.DataFrame()
3
df['a'] = [1, 2, 3, 4, 5]
4
df['b'] = ['C-C02','R-C05','R-C01','C-C06', 'RC-C06']
5
Advertisement
Answer
You could use str.replace
to remove the -
and all characters after it:
JavaScript
1
2
1
df['b'] = df['b'].str.replace(r'-.*$', '', regex=True)
2
Output:
JavaScript
1
7
1
a b
2
0 1 C
3
1 2 R
4
2 3 R
5
3 4 C
6
4 5 RC
7