Как объединить разные DFS и разложить значения? - PullRequest
0 голосов
/ 28 сентября 2019

У меня есть dict, в котором 17 df.

Образец dfs:

df1

          key                percent
0   step19_without_lof  14.534883720930232

df2

           key                   percent
0   step19_without_lof  14.970930232558139

df3

             key             percent
0   step1_without_lof   1.5988372093023255
1   step2_without_lof   30.377906976744185
2   step5_without_lof   3.197674418604651
3   step7_without_lof   9.738372093023257
4   step12_without_lof  5.377906976744186
5   step15_without_lof  4.215116279069767
6   step16_without_lof  6.8313953488372094
7   step19_without_lof  13.80813953488372
8   step24_without_lof  9.883720930232558
9   step25_without_lof  11.337209302325581
10  step26_without_lof  9.738372093023257
11  step27_without_lof  9.738372093023257

и т. Д.

Я хотел бы объединить эти dfs таким образом, чтобы столбец keyстановится именем для каждого столбца, и соответствующие значения заполняются. В df1 и df2, поскольку имеется только один key, оставшиеся keys должны быть заполнены nans.

Желаемый вывод: enter image description here

Как выглядит мой диктат dfs: enter image description here

1 Ответ

1 голос
/ 28 сентября 2019

Вы можете сделать это следующим образом:

# append the key to the index (first level is the old index)
# then unstack the key, so the key is converted to columns
df.set_index('key', append=True).unstack('key')
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...