pandas(二):factorize实现标称型数据数值化

一、factorize()

官网说明

This method is useful for obtaining a numeric representation of an array when all that matters is identifying distinct values. factorize is available as both a top-level function pandas.factorize(), and as a method Series.factorize() and Index.factorize().

pandas.factorize(values, sort=False, order=None, na_sentinel=-1, size_hint=None)
Encode input values as an enumerated type or categorical variable

Parameters:

values:sequence

A 1-D sequence. Sequences that aren’t pandas objects are coerced to ndarrays before factorization.

sort:bool, default False

Sort uniques and shuffle codes to maintain the relationship.

na_sentinel:int or None, default -1

Value to mark “not found”. If None, will not drop the NaN from the uniques of the values.

Changed in version 1.1.2.

size_hint:int, optional

Hint to the hashtable sizer.

Returns

codes:ndarray

An integer ndarray that’s an indexer into uniques. uniques.take(codes) will have the same values as values.

uniques:ndarray, Index, or Categorical

The unique valid values. When values is Categorical, uniques is a Categorical. When values is some other pandas object, an Index is returned. Otherwise, a 1-D ndarray is returned.

个人理解

factorize函数可以将Series中的标称型数据映射称为一组数字,相同的标称型映射为相同的数字。即它把字符串映射成的数字的规则是先看见的小,后看见的大。意思就是这一列的第一行,必定为0,第二行如果和第一行的取值不同,就为1,否则就是0.以此类推。factorize函数的返回值是一个tuple(元组),元组中包含两个元素。第一个元素是一个array,其中的元素是标称型元素映射为的数字;第二个元素是Index类型,其中的元素是所有标称型元素,没有重复。

python实例

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Trisyp

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值