给定以下数据框:
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
关于python-3.x - 根据列名中的匹配字符串对 pandas 单元格(字符串)进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55093597/