[英]Pandas : new column with index of unique values of another column
My dataframe:我的数据框:
ID Name_Identify ColumnA ColumnB ColumnC
1 POM-OPP D43 D03 D59
2 MIAN-ERP D80 D74 E34
3 POM-OPP E97 B56 A01
4 POM-OPP A66 D04 C34
5 DONP28 B55 A42 A80
6 MIAN-ERP E97 D59 C34
Expected new dataframe:预期的新数据框:
ID Name_Identify ColumnA ColumnB ColumnC NEW_ID
1 POM-OPP D43 D03 D59 1
2 MIAN-ERP D80 D74 E34 2
3 POM-OPP E97 B56 A01 1
4 POM-OPP A66 D04 C34 1
5 DONP28 B55 A42 A80 3
6 MIAN-ERP E97 D59 C34 2
您可以使用pandas.Categorical
:
df["NEW_ID"] = pd.Categorical(df["Name_Identify"], ordered=False).codes + 1
You can use pandas.groupby
:您可以使用
pandas.groupby
:
df['NEW_ID'] = df.groupby('Name_Identify', sort=False).ngroup() + 1
print(df)
Prints:印刷:
ID Name_Identify ColumnA ColumnB ColumnC NEW_ID
0 1 POM-OPP D43 D03 D59 1
1 2 MIAN-ERP D80 D74 E34 2
2 3 POM-OPP E97 B56 A01 1
3 4 POM-OPP A66 D04 C34 1
4 5 DONP28 B55 A42 A80 3
5 6 MIAN-ERP E97 D59 C34 2
convert = {k: v for v, k in enumerate(df.Name_Identify.unique(), start=1)}
df["NEW_ID"] = df.Name_Identify.map(convert)
The explanation:说明:
In the first command we select unique names from the Name_Identify
column在第一个命令中,我们从
Name_Identify
列中选择唯一名称
In[23]: df.Name_Identify.unique()
array(['POM-OPP', 'MIAN-ERP', 'DONP28'], dtype=object)
and then create a dictionary from the enumerated sequence of them (the enumeration starts with 1
):然后从它们的枚举序列创建一个字典(枚举以
1
开头):
In[24]: convert = {k: v for v, k in enumerate(df.Name_Identify.unique(), start=1)}
In[25]: convert
{'POM-OPP': 1, 'MIAN-ERP': 2, 'DONP28': 3}
In the second command we use this dictionary for creating a new column by converting all names in the Name_Identify
column to appropriate numbers:在第二个命令中,我们使用此字典通过将
Name_Identify
列中的所有名称转换为适当的数字来创建新列:
In[26]: df["NEW_ID"] = df.Name_Identify.map(convert)
In[27]: df
D Name_Identify ColumnA ColumnB ColumnC NEW_ID 0 1 POM-OPP D43 D03 D59 1 1 2 MIAN-ERP D80 D74 E34 2 2 3 POM-OPP E97 B56 A01 1 3 4 POM-OPP A66 D04 C34 1 4 5 DONP28 B55 A42 A80 3 5 6 MIAN-ERP E97 D59 C34 2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.