如何在Python中为唯一值赋值
df: df_输出:如何在Python中为唯一值赋值,python,python-3.x,pandas,python-2.7,Python,Python 3.x,Pandas,Python 2.7,df: df_输出: Id Products 100 Shoes 101 Shoes 102 Shoes 103 Chair 104 Chair 105 Chair 106 Chair 107 Chair 108 Clothes 109 Clothes 110 Clothes 我需要基于“产品”列创建一个新列,为每个不同的产品分配每个唯一的值。使用factorize Id Products
Id Products
100 Shoes
101 Shoes
102 Shoes
103 Chair
104 Chair
105 Chair
106 Chair
107 Chair
108 Clothes
109 Clothes
110 Clothes
我需要基于“产品”列创建一个新列,为每个不同的产品分配每个唯一的值。使用
factorize
Id Products Value
100 Shoes 1
101 Shoes 1
102 Shoes 1
103 Chair 2
104 Chair 2
105 Chair 2
106 Chair 2
107 Chair 2
108 Clothes 3
109 Clothes 3
110 Clothes 3
输出
df['Value']=df.Products.factorize()[0]+1
使用
factorize
Id Products Value
100 Shoes 1
101 Shoes 1
102 Shoes 1
103 Chair 2
104 Chair 2
105 Chair 2
106 Chair 2
107 Chair 2
108 Clothes 3
109 Clothes 3
110 Clothes 3
输出
df['Value']=df.Products.factorize()[0]+1