pandas replace multiple values one column

后端 未结 6 1737
被撕碎了的回忆
被撕碎了的回忆 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:07

    Looks like OP may have been looking for a one-liner to solve this through consecutive calls to '.str.replace:'

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

    OP, you were close but just needed to replace your commas with .str.replace and the column call ('risk') in a dictionary format isn't necessary. Just pass the pattern-to-match and replacement-value as arguments to replace.

提交回复
热议问题