我正在使用极坐标对数据集中的某些列进行哈希处理。一列包含字符串列表,另一列包含字符串。我的方法是将每列转换为字符串类型,然后对列进行哈希处理。我遇到的问题是类型转换。
我正在使用 with_columns 方法如下:
list_of_lists = [
['base', 'base.current base', 'base.current base.inventories - total', 'ABCD'],
['base', 'base.current base', 'base.current base.inventories - total', 'DEFG'],
['base', 'base.current base', 'base.current base.inventories - total', 'ABCD'],
['base', 'base.current base', 'base.current base.inventories - total', 'HIJK']
]
list_of_strings = ['(bobbyJoe460)',
'bobby, Joe (xx866e)',
'137642039575',
'mamamia']
pl_df_1 = pl.DataFrame({'lists': list_of_lists,'stris':list_of_strings}, strict=False)
pl_df_1.with_columns(pl.col(['lists','stris'])
.cast(pl.List(pl.Categorical))
.hash(seed=140)
.name.suffix('_hashed')
)
请注意,转换是pl.List(pl.Categorical)
。如果我省略转换,则pl.List
转换会失败并显示错误
包含pl.List
代码后得到:
lists stris lists_hashed stris_hashed
list[str] str u64 u64
["base", "base.current base", … "ABCD"] "(bobbyJoe460)" 11845069150176100519 594396677107
["base", "base.current base", … "DEFG"] "bobby, Joe (xx866e)" 6761150988783483050 594396677107
["base", "base.current base", … "ABCD"] "137642039575" 11845069150176100519 594396677107
["base", "base.current base", … "HIJK"] "mamamia" 8290133271651710679 594396677107
请注意,所有字符串列都具有相同的哈希值。理想情况下,我希望在 中有一个布尔表达式,with_columns
用于检测列类型,如果是 List 则使用pl.List(pl.Categorical)
,如果是 String 则使用pl.Categorical
。这可能吗?