pandasfactorize
函数将序列中的每个唯一值赋给一个顺序的、基于0的索引,并计算每个序列项属于哪个索引。
我想在多个列上完成与pandas.factorize
等价的操作:
import pandas as pd
df = pd.DataFrame({'x': [1, 1, 2, 2, 1, 1], 'y':[1, 2, 2, 2, 2, 1]})
pd.factorize(df)[0] # would like [0, 1, 2, 2, 1, 0]
也就是说,我想确定一个数据帧的几列中的每一个唯一的值元组,为每一列分配一个顺序索引,并计算数据帧中每一行属于哪个索引。
Factorize
仅适用于单个列。熊猫有多栏等效函数吗?
您可以使用
drop_duplicates
删除那些重复的行编辑
为了实现您的目标,您可以将原来的df加入drop_duplicated:
我不确定这是否是一个有效的解决方案。也许有更好的解决办法。
所以打印arr会给你
为了保存索引,我将声明一个ind数组
打印ind将给出
首先需要创建一个元组数组,
pandas.lib.fast_zip
在cython循环中可以非常快地完成此操作。输出为:
相关问题 更多 >
编程相关推荐