Сортировать ячейки панд (строки) по соответствующей строке в имени столбца - PullRequest
2 голосов
/ 11 марта 2019

Учитывая следующий фрейм данных:

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 Ответ

2 голосов
/ 11 марта 2019

Я думаю, вы можете перестроить свой фрейм данных

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
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...