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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36646923/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 01:03:17  来源:igfitidea点击:

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

pythonpandas

提问by ??????

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 B-C combination. That is, the output should be

我需要创建一个 ID 变量,它对每个 BC 组合都是唯一的。也就是说,输出应该是

            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. I just want something fast, that does not take a lot of memory and can be sorted quickly. I use:

我实际上并不关心索引是否从零开始,以及缺失列的值是 0 还是任何其他数字。我只是想要一些快速的东西,它不需要很多内存并且可以快速排序。我用:

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

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

但输出是float64并且需要大量内存。我们能做得更好吗?谢谢!

回答by jezrael

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

回答by Nolan Conaway

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']])