Remap values in pandas column with a dict

后端 未结 10 1123
囚心锁ツ
囚心锁ツ 2020-11-21 05:14

I have a dictionary which looks like this: di = {1: \"A\", 2: \"B\"}

I would like to apply it to the \"col1\" column of a dataframe similar to:

10条回答
  •  猫巷女王i
    2020-11-21 05:47

    A nice complete solution that keeps a map of your class labels:

    labels = features['col1'].unique()
    labels_dict = dict(zip(labels, range(len(labels))))
    features = features.replace({"col1": labels_dict})
    

    This way, you can at any point refer to the original class label from labels_dict.

提交回复
热议问题