在熊猫数据框中为给定的列值创建顺序ID

时间:2019-07-10 04:42:30

标签: python pandas

我想为“类型”列中的每个给定值创建一个唯一的顺序ID,但似乎无法使其正常工作。

当前数据帧:

    Item    Type    ID
0   Apple   Fruit   0
1   Orange  Fruit   1
2   Banana  Fruit   2
3   Peach   Fruit   3
4   Cheese  Dairy   0
5   Milk    Dairy   1
6   Chicken Meat    0
7   Pork    Meat    1
8   Beef    Meat    2

所需的数据帧:

    Item    Type    ID
0   Apple   Fruit   0
1   Orange  Fruit   1
2   Banana  Fruit   2
3   Peach   Fruit   3
4   Cheese  Dairy   0
5   Milk    Dairy   1
6   Chicken Meat    0
7   Pork    Meat    1
8   Beef    Meat    2

我尝试设置set_index并创建一个单独的列,该列指示“ Type”值的更改,但无法创建所需的格式。任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:1)

尝试使用cumcount()

df = pd.DataFrame(data={"Type":["Fruit","Fruit","Dairy","Meat","Meat"],
                        "Item":["Apple","Orange","Chesse","Pork","Beef"]})

df["ID"] = df.groupby(['Type']).cumcount()
print(df)
    Type    Item  ID
0  Fruit   Apple   0
1  Fruit  Orange   1
2  Dairy  Chesse   0
3   Meat    Pork   0
4   Meat    Beef   1

我希望它能解决您的问题