简体   繁体   English

Python:如何将数字映射到列中的唯一项(枚举唯一对象)?

[英]Python: How to map numbers to unique items (enumerate unique objects) in a column?

I need to assign numbers to unique values from a specific column in a dataframe, and make it as a new column...我需要为数据框中特定列的唯一值分配数字,并将其作为新列...

For example:例如:

ex_list=['12-B', '10002', '3A', '4', 'DCX', '3A']
df=pd.DataFrame(ex_list, columns=['Items'])

我的数据框

This is the expected output :这是预期的输出

预期数据框

Please note that I need to do this for a dataframe with over 1M rows, so definitely would appreciate an efficient approach!请注意,我需要对超过 100 万行的数据帧执行此操作,因此绝对会欣赏一种有效的方法!

You can do it like this.你可以这样做。

import pandas as pd
ex_list=['12-B', '10002', '3A', '4', 'DCX', '3A']
df=pd.DataFrame(ex_list, columns=['Items'])
df['Num_Items']=pd.factorize(df['Items'].tolist())[0]

Output输出

    Items   Num_Items
0   12-B    0
1   10002   1
2   3A      2
3   4       3
4   DCX     4
5   3A      2
ex_list=['12-B', '10002', '3A', '4', 'DCX', '3A']
df=pd.DataFrame(ex_list, columns=['Items'])

Get the unique values from your column, create a mapping between those unique items and integers and then apply that mapping along the Items column.从您的列中获取唯一值,在这些唯一项和整数之间创建映射,然后沿Items列应用该映射。

mapping = {item:i for i, item in enumerate(df["Items"].unique())}
df["Num_Items"] = df["Items"].apply(lambda x: mapping[x])
df

期望的输出

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM