python - 在熊猫数据框中拆分字符串
问题描述
我的数据框中有此列,其中包含字符串中的数字,"6,22,67,82"
例如。我想将此字符串拆分为整数数组并将数组保留在数据框中。
h['htgt']=h['htgt'].split()
这不起作用,因为它试图拆分整个系列。
解决方案
您可以使用pd.Series.str.split
withexpand=True
然后转换为int
. 假设每个字符串中有相同数量的数字。
h = pd.DataFrame({'htgt': ['6,22,67,82', '12,45,65,14', '54,15,9,94']})
res = h['htgt'].str.split(',', expand=True).astype(int)
print(res)
0 1 2 3
0 6 22 67 82
1 12 45 65 14
2 54 15 9 94
推荐阅读
- javascript - SweetAlert2 似乎对我不起作用。我做错了什么?
- javascript - 如何使用 Javascript 将 EJS 标签替换为另一个 EJS 标签
- loops - 使用相同的用户详细信息重新启动 Jmeter 线程组
- python - 从输入图像裁剪或覆盖面罩中的多边形到输出形状因子
- r - 如何存储需要从多个 R 项目访问的数据,同时保持代码可移植性?
- php - Laravel问题从电子邮件验证传递错误消息
- php - 在 PHP 应用程序的 docker 容器中使用 docker secrets
- rdf - OWL Ontology 中子类级别的推理关系
- c# - SQL Server (ORM) 从数据库中选择数据 - System.InvalidCastException
- drupal - 访问 $form_state 并从中获取数据