gpt4 book ai didi

python - 将 pandas 系列从字符串转换为唯一的 int id

转载 作者:太空宇宙 更新时间:2023-11-03 20:25:23 25 4
gpt4 key购买 nike

我有一个系列中的分类变量。我想为每个唯一值分配整数 id,并使用 id 创建一个新系列,从而有效地将字符串变量转换为整数变量。最紧凑/最有效的方法是什么?

最佳答案

您可以使用pandas.factorize :

In [32]: s = pd.Series(['a','b','c'])

In [33]: labels, levels = pd.factorize(s)

In [35]: labels
Out[35]: array([0, 1, 2])

关于python - 将 pandas 系列从字符串转换为唯一的 int id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57862394/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com