python - 如何在python pandas中将值范围更改为实际值
问题描述
我在 pandas 数据框中有数据,其中列值看起来像 732-738 我想将这些值范围扩展到单独的行中,这样做也可以扩展下一列值,任何人都可以建议我这样做吗?
Destination ZIP Zone
732-738 8
005 7
解决方案
这将完成这项工作
df['Destination']=df['Destination'].apply(lambda x: range([int(i) for i in x.split('-')][0],[int(i) for i in x.split('-')][1]+1) if "-" in x else x)
df.explode('Destination')
Destination_ZIP Zone
0 732 8
0 733 8
0 734 8
0 735 8
0 736 8
0 737 8
0 738 8
1 005 7
推荐阅读
- php - 以分钟为单位计算时间与当前时间的差
- java - 运行 Selenium 会话时自定义浏览器图标?
- pytorch - Pytorch resnet 零损失
- python - 解析嵌套字典(allen nlp hierplane_tree)
- javascript - 随机数生成器不能与 parseInt 一起使用?
- python - 模式的正则表达式
- django - 如何使用 Firebase 和 Django Rest 验证 ID 令牌?
- c++ - C++如何用views::split打印单词?
- npm - npm audit --parseable 的列名文档?
- python - 无法找出我的 jinja2/django 模板上的错误