擅长:python、mysql、java
<p>使用<a href="http://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.factorize.html" rel="nofollow noreferrer">^{<cd1>}</a>并添加<a href="http://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.insert.html" rel="nofollow noreferrer">^{<cd2>}</a>:</p>
<pre><code>df.insert(1, 'Mask_ProductId', pd.factorize(df['ProductId'])[0])
print (df)
ProductId Mask_ProductId Sales
0 AXX11 0 10
1 CS22 1 34
2 AXX11 0 23
3 FV34 2 45
4 FV34 2 23
5 DF23 3 54
6 CS22 1 65
</code></pre>