Skip to content Skip to sidebar Skip to footer

Replace String/value In Entire Dataframe

I have a very large dataset were I want to replace strings with numbers. I would like to operate on the dataset without typing a mapping function for each key (column) in the datas

Solution 1:

Use replace

In[126]: df.replace(['very bad', 'bad', 'poor', 'good', 'very good'], 
                     [1, 2, 3, 4, 5]) 
Out[126]: 
      respABC013341243423555342324511156341674447855589221910111

Solution 2:

Considering data is your pandas DataFrame you can also use:

data.replace({'very bad': 1, 'bad': 2, 'poor': 3, 'good': 4, 'very good': 5}, inplace=True)

Solution 3:

data = data.replace(['very bad', 'bad', 'poor', 'good', 'very good'], [1, 2, 3, 4, 5])

You must state where the result should be saved. If you say only data.replace(...) it is only shown as a change in preview, not in the envirable itself.

Post a Comment for "Replace String/value In Entire Dataframe"