python-3.x - 根据列名中的匹配字符串对熊猫单元格(字符串)进行排序
问题描述
给定以下数据框:
df = pd.DataFrame({'doc' : ['2739','2697','3135','896'],
'tool' : ["system: 15", "architectur: 5" ,"tool: 10", "tool: 11"],
'system' : ["tool: 1", "tool: 3" , "system: 5", "system: 14"],
'architectur' : ["architectur: 4", "system: 28", "architectur: 3", "architectur: 10"]})
df = df.set_index('doc')
print(df)
tool system architectur
doc
2739 system: 15 tool: 1 architectur: 4
2697 architectur: 5 tool: 3 system: 28
3135 tool: 10 system: 5 architectur: 3
896 tool: 11 system: 14 architectur: 10
我正在尝试根据列名中的匹配字符串重新排序字符串。
最终目标是获得这个:
tool system architectur
doc
2739 tool: 1 system: 15 architectur: 4
2697 tool: 3 system: 28 architectur: 5
3135 tool: 10 system: 5 architectur: 3
896 tool: 11 system: 14 architectur: 10
提前致谢!
解决方案
我认为您可以重新构建数据框
yourdf=pd.DataFrame([dict(map(tuple,[y.split(':') for y in x ])) for x in (df.values.tolist())],index=df.index)
yourdf
Out[159]:
architectur system tool
doc
2739 4 15 1
2697 5 28 3
3135 3 5 10
896 10 14 11
推荐阅读
- jenkins - https://jenkins.io 的部分内容已关闭。我该如何报告?
- c++ - 获取每个目录或子目录中第一个文件的路径
- performance - SQL Developer File Open & Export 显示对话框很慢
- c++ - Qt/C++ 模拟 QSettings
- c# - 将字符串数组传递给 Redshift
- bash - 在 bash 中使用 JSON 文件的名称及其内容创建 JSON 对象
- wordpress - 从 Woocommerce 的管理员列表中发送选定的订单通知电子邮件
- angular - 如何避免 Angular 库中的多个服务实例?(带有延迟加载的模块)
- firebase - Firestore 安全规则是否会提前返回?
- html - 大纲 html5 音频元素