Home > Mobile >  How to rename dataframe items according to string and numeric condition?
How to rename dataframe items according to string and numeric condition?

Time:01-06

I have the following dataframe:

import pandas as pd
import re

df = pd.DataFrame({'Name': ['Paris', 'New York', '3 Pontas', '25466', '1', '4071'],                   
                   'Lat': [48.85, 40.73, -22.92, 22.85, 44.54, 55.87],                   
                  })


print(df)

          Name           Lat
         Paris         48.85
        New York       40.73
       3 Pontas       -22.92
          25466        22.85
              1        44.54
           4071        55.87

I need to replace names that are formed by numbers only with empty space. I tried to implement the following code:

for i in range(0, len(df)):    
    contains_digit = len(re.findall('\d ', df['Name'].iloc[i]))

    if(contains_digit > 0):
        df['Name'].iloc[i] = ''

The output generated by my implementation is:

 print(df)

          Name           Lat
         Paris         48.85
        New York       40.73
                      -22.92
                       22.85
                       44.54
                       55.87

However, I need to keep the names that have a number in the name as well. Thus, the desired output is:

          Name           Lat
         Paris         48.85
        New York       40.73
       3 Pontas       -22.92
                       22.85
                       44.54
                       55.87

CodePudding user response:

Solution with Series.str.isnumeric:

df.loc[df['Name'].str.isnumeric(), 'Name'] = ''

print(df)

       Name    Lat
0     Paris  48.85
1  New York  40.73
2  3 Pontas -22.92
3            22.85
4            44.54
5            55.87

CodePudding user response:

Another way; replace strings containing only digits

df['Name'] =df['Name'].str.replace('^\d $','', regex=True)



       Name    Lat
0     Paris  48.85
1  New York  40.73
2  3 Pontas -22.92
3            22.85
4            44.54
5            55.87
  •  Tags:  
  • Related