python-3.x - 散列 pandas Dataframe 列中字符串列表的每个元素
问题描述
所以我有一个这样的数据框
customer location trip_len
0 a ['1', '2', '3'] 3
1 b ['4', '5'] 2
而且我正在尝试对每一行,列“位置”上的字符串列表中的每个元素进行哈希处理,以提高模型的效率。这怎么可能?
解决方案
列表本身不是可散列的,因为它们是可变对象。如果仅对存储感兴趣,则可以选择转换为 a tuple
。
df.location = df.location.apply(lambda locations: hash(tuple(locations)))
推荐阅读
- webstorm - WebStorm - 在花括号内缩进 JSX 元素
- datatable - 防止数据表崩溃
- java - 如果 String 包含大于 Integer.MAX_VALUE 的数字
- javascript - APIC 2018:如何在网关脚本中读取来自调用策略的 XML 响应
- html - 在visualforce中,CSS样式的高度不适用于表格
- websphere - 集群环境中 EJB 计时器服务的自定义调度程序实例中的问题
- json - 我怎样才能从我的对象中只发布一个元素?
- r - 超链接闪亮用户界面下拉菜单的标题?
- python - heartbleed PoC ssltest,py 不起作用,socket.send 错误:管道损坏
- typescript - 输入 lodash 函数的正确方法是什么?