Home > Net >  How to efficiently remove #| on multiple rows
How to efficiently remove #| on multiple rows

Time:08-08

I have been trying to replace function and regex expression to remove the digit | while keeping the country intact.

['74 | United Kingdom' '54 | Germany' '136 | Monaco' '62 | Egypt' nan
 '155 | Malaysia' '161 | Nigeria' '68 | Fiji' '176 | Poland'
 '228 | United States' '103 | India' '121 | Kuwait' '23 | Bahrain'
 '243 | South Africa'

Not sure how to get rid of the numbers without going through every single country. Please advise.

Only have doing df = df.replace('243 | South Africa', 'South Africa')

CodePudding user response:

Use a regex:

df['your_column'] = df['your_column'].str.replace(r'^[\d\s\|]*', '', regex=True)

regex:

^          # match start of string
[\d\s\|]*  # match any number of digit/space/|
  • Related