简体   繁体   English

Pandas:具有另一列唯一值索引的新列

[英]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.

相关问题 给定列的唯一索引值的新列 - New column of unique index values for a given column 从另一列创建新列 + pandas dataframe 中的唯一数字索引 - Creating a new column from another column + unique numeric index in pandas dataframe 如何将唯一列值取消堆叠到列并将另一列设置为 Python Pandas 中的行索引 - How to unstack unique column values to columns and set another column as row index in Python Pandas Pandas:具有基于条件的唯一值的新列 - Pandas: new column with unique values based on condition 根据熊猫中的另一列对唯一值进行排序 - Sorting unique values according to another column in pandas 在 Pandas 数据框中创建一个新的列表列,其中包含来自另一列的唯一值 - Create a new column of lists in Pandas dataframe with unique values from another column 在与另一列Pandas中的唯一值相关联的列中查找值的交集 - Finding intersection of values in a column associated with unique values in another column Pandas 基于唯一的多列索引的另一个DataFrame的新pandas DataFrame - New pandas DataFrame from another DataFrame based on a unique multiple column index Pandas 根据现有列的第一个唯一值创建新列 - Pandas create new column based on first unique values of existing column 基于现有列的唯一值(列表)的 Pandas DataFrame 中的新列 - new column in pandas DataFrame based on unique values (lists) of an existing column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM