python pandas replacing strings in dataframe with numbers

前端 未结 9 1548
名媛妹妹
名媛妹妹 2020-12-08 04:22

Is there anyway to use the mapping function or something better to replace values in an entire dataframe?

I only know how to perform the mapping on series.

I

相关标签:
9条回答
  • 2020-12-08 04:45

    What about DataFrame.replace?

    In [9]: mapping = {'set': 1, 'test': 2}
    
    In [10]: df.replace({'set': mapping, 'tesst': mapping})
    Out[10]: 
       Unnamed: 0 respondent  brand engine  country  aware  aware_2  aware_3  age  \
    0           0          a  volvo      p      swe      1        0        1   23   
    1           1          b  volvo   None      swe      0        0        1   45   
    2           2          c    bmw      p       us      0        0        1   56   
    3           3          d    bmw      p       us      0        1        1   43   
    4           4          e    bmw      d  germany      1        0        1   34   
    5           5          f   audi      d  germany      1        0        1   59   
    6           6          g  volvo      d      swe      1        0        0   65   
    7           7          h   audi      d      swe      1        0        0   78   
    8           8          i  volvo      d       us      1        1        1   32   
    
      tesst set  
    0     2   1  
    1     1   2  
    2     2   1  
    3     1   2  
    4     2   1  
    5     1   2  
    6     2   1  
    7     1   2  
    8     2   1  
    

    As @Jeff pointed out in the comments, in pandas versions < 0.11.1, manually tack .convert_objects() onto the end to properly convert tesst and set to int64 columns, in case that matters in subsequent operations.

    0 讨论(0)
  • 2020-12-08 04:50

    When no of features are not much :

    mymap = {'a':1, 'b':2, 'c':3, 'd':4, 'e':5}
    df.applymap(lambda s: mymap.get(s) if s in mymap else s)
    

    When it's not possible manually :

    temp_df2 = pd.DataFrame({'data': data.data.unique(), 'data_new':range(len(data.data.unique()))})# create a temporary dataframe 
    data = data.merge(temp_df2, on='data', how='left')# Now merge it by assigning different values to different strings.
    
    0 讨论(0)
  • 2020-12-08 04:55

    The simplest way to replace any value in the dataframe:

    df=df.replace(to_replace="set",value="1")
    df=df.replace(to_replace="test",value="2")
    

    Hope this will help.

    0 讨论(0)
提交回复
热议问题