Pandas replace with default value
问题 I have a pandas dataframe I want to replace a certain column conditionally. eg: col 0 Mr 1 Miss 2 Mr 3 Mrs 4 Col. I want to map them as {'Mr': 0, 'Mrs': 1, 'Miss': 2} If there are other titles now available in the dict then I want them to have a default value of 3 The above example becomes col 0 0 1 2 2 0 3 1 4 3 Can I do this with pandas.replace() without using regex ? 回答1: You can use map rather as replace , because faster, then fillna by 3 and cast to int by astype: df['col'] = df.col.map(