Reputation: 1
I want to remove everything after '-' in each row in one column in a pandas dataframe. I have tried str.split to no avail.
Upvotes: 0
Views: 60
Reputation: 210882
Try this:
df['column'] = df['column'].str.replace(r'-.*$', '')
Demo:
In [154]: df
Out[154]:
column
0 aaa
1 asd-bfd-asd
2 -xsdert-...
3 123-345
In [155]: df['column'] = df['column'].str.replace(r'-.*$', '')
In [156]: df
Out[156]:
column
0 aaa
1 asd
2
3 123
or using .str.split()
:
In [159]: df['column'] = df['column'].str.split('-').str[0]
In [160]: df
Out[160]:
column
0 aaa
1 asd
2
3 123
Upvotes: 1