user3609179
user3609179

Reputation: 301

Conditional strip values left of delimiter pandas column

I have a column in a pandas DF that looks like :

Column
30482304823
3204820
2304830
Apple   - 390483204
Orange - 3939491
grape - 34038414
apple

I want to remove everything to the left of the '-' so basically I want the above to look like :

Column
30482304823
3204820
2304830
390483204
3939491
34038414
apple

I have tried the following pandas snippets :

out['Column'] = out['Column'].str.split('-', 1, expand=True)[1]
out['Column'] = out['Column'].str.replace('Orange -', '', )
out['Column'].str.map(lambda x: x.lstrip('Orange -'))
out['Column'].str.lstrip('Orange -') 

Upvotes: 1

Views: 941

Answers (2)

enterML
enterML

Reputation: 2285

out['Column'] = out['Column'].apply(lambda x : str(x).split('-')[-1])

Upvotes: 1

piRSquared
piRSquared

Reputation: 294348

Simplest I can think of is

df.Column.str.split('\s*-\s*').str[-1]

0    30482304823
1        3204820
2        2304830
3      390483204
4        3939491
5       34038414
6          apple
Name: Column, dtype: object

Upvotes: 3

Related Questions