首页 > 解决方案 > 散列 pandas Dataframe 列中字符串列表的每个元素

问题描述

所以我有一个这样的数据框

   customer     location       trip_len
0     a      ['1', '2', '3']      3
1     b      ['4', '5']           2

而且我正在尝试对每一行,列“位置”上的字符串列表中的每个元素进行哈希处理,以提高模型的效率。这怎么可能?

标签: python-3.xpandaslisthash

解决方案


列表本身不是可散列的,因为它们是可变对象。如果仅对存储感兴趣,则可以选择转换为 a tuple

df.location = df.location.apply(lambda locations: hash(tuple(locations)))

推荐阅读