Панды преобразовывают несколько столбцов в один столбец, преобразовывают столбец в переменные - PullRequest
0 голосов
/ 23 октября 2018

У меня есть датафрейм, подобный приведенному ниже:

    Year    Scenario    Type_1  US  China   India   France  Type_2  US  India Germany
0   2010    A           Type_1  100   101    102     100    Type_2  50   51   52
1   2011    A           Type_1  102   103    104     102    Type_2  52   53   54
2   2010    B           Type_1  99    100    101     99     Type_2  49   50   51
3   2011    B           Type_1  101   102    103     101    Type_2  51   52   53

Я хотел бы преобразовать столбцы в переменные.Возможно ли сделать так, чтобы датафрейм выглядел так, как показано ниже?

    Year    Scenario    Type    Country Value
0   2010    A           Type_1  US      100
1   2010    A           Type_1  China   101
2   2010    A           Type_1  India   102
3   2010    A           Type_1  France  100
4   2011    A           Type_1  US      102
5   2011    A           Type_1  China   103
6   2011    A           Type_1  India   104
7   2011    A           Type_1  France  102
8   2010    B           Type_1  US       99
9   2010    B           Type_1  China   100
10  2010    B           Type_1  India   101
11  2010    B           Type_1  France  99
12  2011    B           Type_1  US      101
13  2011    B           Type_1  China   102
14  2011    B           Type_1  India   103
15  2011    B           Type_1  France  101
16  2010    A           Type_2  US      50
17  2010    A           Type_2  India   51
18  2010    A           Type_2  Germany 52
19  2011    A           Type_2  US      52
20  2011    A           Type_2  India   53
21  2011    A           Type_2  Germany 54
22  2010    B           Type_2  US      49
23  2010    B           Type_2  India   50
24  2010    B           Type_2  Germany 51
25  2011    B           Type_2  US      51
26  2011    B           Type_2  India   52
27  2011    B           Type_2  Germany 53

1 Ответ

0 голосов
/ 23 октября 2018

немного сложный вопрос, так как у вас есть дубликат имени в столбцах.

s1=df.melt(['Year','Scenario','Type_1','Type_2']) 
# using the melt , and now we are missing the Type column
s2=df.filter(like='Type').reindex(columns=df.columns).ffill(1).melt(['Year','Scenario','Type_1','Type_2'])
# we using mask and ffill to get the type fill in all countries' value , then the dataframe is in the same shape like before , we melt again , this time the value column is the type column 
s1['Type']=s2.value
s1=s1.drop(['Type_1','Type_2'],1)
s1
Out[107]: 
    Year Scenario variable  value    Type
0   2010        A       US    100  Type_1
1   2011        A       US    102  Type_1
2   2010        B       US     99  Type_1
3   2011        B       US    101  Type_1
4   2010        A    China    101  Type_1
5   2011        A    China    103  Type_1
6   2010        B    China    100  Type_1
7   2011        B    China    102  Type_1
8   2010        A    India    102  Type_1
9   2011        A    India    104  Type_1
10  2010        B    India    101  Type_1
11  2011        B    India    103  Type_1
12  2010        A   France    100  Type_1
13  2011        A   France    102  Type_1
14  2010        B   France     99  Type_1
15  2011        B   France    101  Type_1
16  2010        A       US     50  Type_2
17  2011        A       US     52  Type_2
18  2010        B       US     49  Type_2
19  2011        B       US     51  Type_2
20  2010        A    India     51  Type_2
21  2011        A    India     53  Type_2
22  2010        B    India     50  Type_2
23  2011        B    India     52  Type_2
24  2010        A  Germany     52  Type_2
25  2011        A  Germany     54  Type_2
26  2010        B  Germany     51  Type_2
27  2011        B  Germany     53  Type_2
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...