Claire
Claire

Reputation: 1

Removing part of a string in every column in pandas dataframe after a symbol

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

Answers (1)

MaxU - stand with Ukraine
MaxU - stand with Ukraine

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

Related Questions