简体   繁体   English

Pandas,如何根据多个列的组合创建一个唯一的ID?

[英]In Pandas, how to create a unique ID based on the combination of many columns?

I have a very large dataset, that looks like我有一个非常大的数据集,看起来像

df = pd.DataFrame({'B': ['john smith', 'john doe', 'adam smith', 'john doe', np.nan], 'C': ['indiana jones', 'duck mc duck', 'batman','duck mc duck',np.nan]})

df
Out[173]: 
            B              C
0  john smith  indiana jones
1    john doe   duck mc duck
2  adam smith         batman
3    john doe   duck mc duck
4         NaN            NaN

I need to create a ID variable, that is unique for every BC combination.我需要创建一个 ID 变量,它对于每个 BC 组合都是唯一的。 That is, the output should be也就是说,output应该是

            B              C   ID
0  john smith  indiana jones   1
1    john doe   duck mc duck   2
2  adam smith         batman   3
3    john doe   duck mc duck   2 
4         NaN            NaN   0

I actually dont care about whether the index starts at zero or not, and whether the value for the missing columns is 0 or any other number.我实际上不关心索引是否从零开始,以及缺失列的值是 0 还是任何其他数字。 I just want something fast, that does not take a lot of memory and can be sorted quickly.我只是想要一些快速的东西,不需要很多 memory 并且可以快速排序。 I use:我用:

df['combined_id']=(df.B+df.C).rank(method='dense')

but the output is float64 and takes a lot of memory. Can we do better?但是 output 是float64并且需要很多 memory。我们可以做得更好吗? Thanks!谢谢!

I think you can use factorize :我认为你可以使用factorize

df['combined_id'] = pd.factorize(df.B+df.C)[0]
print df
            B              C  combined_id
0  john smith  indiana jones            0
1    john doe   duck mc duck            1
2  adam smith         batman            2
3    john doe   duck mc duck            1
4         NaN            NaN           -1

Making jezrael's answer a little more general (what if the columns were not string?), you can use this compact function:使 jezrael 的答案更笼统一些(如果列不是字符串怎么办?),您可以使用此紧凑函数:

def make_identifier(df):
    str_id = df.apply(lambda x: '_'.join(map(str, x)), axis=1)
    return pd.factorize(str_id)[0]

df['combined_id'] = make_identifier(df[['B','C']])

I have a very large dataset, that looks like我有一个非常大的数据集,看起来像

df = pd.DataFrame({'B': ['john smith', 'john doe', 'adam smith', 'john doe', np.nan], 'C': ['indiana jones', 'duck mc duck', 'batman','duck mc duck',np.nan]})

df
Out[173]: 
            B              C
0  john smith  indiana jones
1    john doe   duck mc duck
2  adam smith         batman
3    john doe   duck mc duck
4         NaN            NaN

I need to create a ID variable, that is unique for every BC combination.我需要创建一个ID变量,该变量对于每个BC组合都是唯一的。 That is, the output should be也就是说,输出应为

            B              C   ID
0  john smith  indiana jones   1
1    john doe   duck mc duck   2
2  adam smith         batman   3
3    john doe   duck mc duck   2 
4         NaN            NaN   0

I actually dont care about whether the index starts at zero or not, and whether the value for the missing columns is 0 or any other number.我实际上并不在乎索引是否从零开始,以及缺失列的值是0还是任何其他数字。 I just want something fast, that does not take a lot of memory and can be sorted quickly.我只想要快速的东西,不需要太多的内存并且可以快速排序。 I use:我用:

df['combined_id']=(df.B+df.C).rank(method='dense')

but the output is float64 and takes a lot of memory.但输出为float64并占用大量内存。 Can we do better?我们可以做得更好吗? Thanks!谢谢!

jezrael's answer is great. jezrael 的回答很棒。 But since this is for multiple columns, I prefer to use .ngroup() since this way NaN could remain NaN.但是因为这是针对多列的,所以我更喜欢使用.ngroup()因为这样 NaN 可以保持为 NaN。

df['combined_id'] = df.groupby(['B', 'C'], sort = False).ngroup()
df

在此处输入图像描述

暂无
暂无

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

相关问题 如何基于组合1和许多列在Pandas DataFrame中创建新列 - How to create a new column in Pandas DataFrame based on a combination 1 and many columns 在Pandas中,如何根据其他列的公共相互关系创建唯一ID? - In Pandas, how to create a unique ID based on the common interrelation of other columns? 如何根据 pandas 中其他两列的唯一组合获得两列的唯一计数 - How to get unique count of two columns based on unique combination of other two columns in pandas 根据列组合在 dataframe 中创建唯一标识符 - create unique identifier in dataframe based on combination of columns 在 Pandas 中基于列重复数据删除创建 unique_id - Create unique_id based in columns deduplication in Pandas 将唯一 ID 分配给 Pandas 数据框中两列的组合,按其顺序独立 - Assign unique ID to combination of two columns in pandas dataframe independently on their order 根据列组合在 dataframe 中创建唯一标识符,但仅适用于重复行 - create unique identifier in dataframe based on combination of columns, but only for duplicated rows 如何根据python数据框中两列的组合创建唯一代码 - How to create unique code based on combination of two columns in python data frame 如何在 pandas 中按两列分组,其中两者的组合是唯一的 - How to group by two columns in pandas where the combination of the two is unique 如何为熊猫列中的唯一值创建行? - How to create rows for unique values in columns in pandas?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM