Map string values in a Pandas Dataframe with integers

前端 未结 1 1655
深忆病人
深忆病人 2021-01-18 04:30

In Pandas DataFrame how to map strings in one column with integers. I have around 500 strings in the DataFrame and need to replace them with intege

相关标签:
1条回答
  • 2021-01-18 05:07

    So what you could do is construct a temporary dataframe and merge this back to your existing dataframe:

    temp_df = pd.DataFrame({'Request': df.Request.unique(), 'Request_id':range(len(df.Request.unique()))})
    

    Now merge this back to your original dataframe

    df = df.merge(temp_df, on='Request', how='left')
    
    0 讨论(0)
提交回复
热议问题