2

I have a dataframe in pandas, and in a column have a lot of names with numbers. How do I remove the number? example: Andorra14:

energy['Country'].head()  
1.       Afghanistan   
2.           Albania   
3.           Algeria   
4.    American Samoa    
5.         Andorra14   
Name: Country, dtype: object
FLab
  • 7,136
  • 5
  • 36
  • 69
Pedro Teixeira
  • 59
  • 1
  • 1
  • 7

2 Answers2

5

The str attribute is your friend:

In [10]: energy['Country'].str.replace('\d+', '')
Out[10]: 
0       Afghanistan
1           Albania
2           Algeria
3    American Samoa
4           Andorra
Name: Country, dtype: object
chrisaycock
  • 36,470
  • 14
  • 88
  • 125
  • 1
    just be careful that your numbers are not between two words using `str.replace`: if you have `American12Samoa` it will become `AmericanSamoa`. If you replace with white space then you could have `'Andorra12'` to `'Andorra '` – It_is_Chris Aug 22 '18 at 15:02
4

My guess would be using regex:

energy['Country'] = energy['Country'].str.replace(r'\d+','')

From:

0    Afghani1stan
1        Alb3ania
2         Algeria
3  American Samoa
4       Andorra14

I got:

0     Afghanistan
1         Albania
2         Algeria
3  American Samoa
4         Andorra
ALollz
  • 57,915
  • 7
  • 66
  • 89