根据列名称中的匹配字符串对熊猫细胞(字符串)进行排序

时间:2019-03-10 23:50:33

标签: python-3.x string pandas sorting

给出以下数据框:

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

谢谢!

1 个答案:

答案 0 :(得分:2)

我认为您可以重新构建数据框

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