在 pandas 中,如何基于多个列的组合创建唯一的ID? [英] In Pandas, how to create a unique ID based on the combination of many columns?

查看:44
本文介绍了在 pandas 中,如何基于多个列的组合创建唯一的ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常大的数据集,看起来像

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

我需要创建一个ID变量,该变量对于每个B-C组合都是唯一的.也就是说,输出应为

I need to create a ID variable, that is unique for every B-C combination. 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

我实际上并不关心索引是否从零开始,以及缺少的列的值是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. 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')

,但输出为float64,并占用大量内存.我们可以做得更好吗? 谢谢!

but the output is float64 and takes a lot of memory. Can we do better? Thanks!

推荐答案

我认为您可以使用

I think you can use 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

这篇关于在 pandas 中,如何基于多个列的组合创建唯一的ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆