pandas replace multiple values one column

后端 未结 6 1753
被撕碎了的回忆
被撕碎了的回忆 2020-11-30 07:43

In a column risklevels I want to replace Small with 1, Medium with 5 and High with 15. I tried:

dfm.replace({\'risk\':{\'Small\': \'1\'}},{\'risk\':{\'Medium         


        
6条回答
  •  忘掉有多难
    2020-11-30 08:09

    String replace each string (Small, Medium, High) for the new string (1,5,15)\

    If dfm is the dataframe name, column is the column name.

    dfm.column = dfm.column.str.replace('Small', '1')
    dfm.column = dfm.column.str.replace('Medium', '5')
    dfm.column = dfm.column.str.replace('High', '15')
    

提交回复
热议问题