-3

I am trying to remove the numbers before "-" in the name column. But not all rows have numbers before the name. How do I remove the numbers in rows that have numbers and keep the rows that don't have numbers in front untouched?

Sample df:

country     Name
UK          5413-Marcus
Russia      5841-Natasha
Hong Kong   Keith
China       7777-Wang

Desired df

country     Name
UK          Marcus
Russia      Natasha
Hong Kong   Keith
China       Wang

I appreciate any assistance! Thanks in advance!

Matthias Gallagher
  • 475
  • 1
  • 7
  • 20

2 Answers2

2

Pandas has string accessors for series. If you split and get the last element of the resulting list, even if a row does not have the delimeter '-' you still want the last element of that one-element list.

df.Name = df.Name.str.split('-').str.get(-1)
RichieV
  • 5,103
  • 2
  • 11
  • 24
1

You might use str.lstrip for that task following way:

import pandas as pd
df = pd.DataFrame({'country':['UK','Russia','Hong Kong','China'],'Name':['5413-Marcus','5841-Natasha','Keith','7777-Wang']})
df['Name'] = df['Name'].str.lstrip('-0123456789')
print(df)

Output:

     country     Name
0         UK   Marcus
1     Russia  Natasha
2  Hong Kong    Keith
3      China     Wang

.lstrip does remove leading characters, .rstrip trailing characters and .strip both.

Daweo
  • 31,313
  • 3
  • 12
  • 25