将列名称变为值的多索引pandas数据框展平

时间:2017-09-19 17:44:50

标签: python pandas

我们说我有以下数据框:

import pandas as pd
df = pd.DataFrame(data={'Status' : ['green','green','red','blue','red','yellow','black'],
 'Group' : ['A','A','B','C','A','B','C'],
 'City' : ['Toronto','Montreal','Vancouver','Toronto','Edmonton','Winnipeg','Windsor'],
 'Sales' : [13,6,16,8,4,3,1]})
df.drop('Status',axis=1,inplace=True)
ndf = pd.pivot_table(df,values=['Sales'],index=['City'],columns=['Group'],fill_value=0,margins=False)

结果如下:

In [321]: ndf
Out[321]:
          Sales
Group         A   B  C
City
Edmonton      4   0  0
Montreal      6   0  0
Toronto      13   0  8
Vancouver     0  16  0
Windsor       0   0  1
Winnipeg      0   3  0

如何将其展平以使其成为单级数据框但是使用指定该组的列?

即,结果应该是:

City    group   sales
Edmonton    A   4
Edmonton    B   0
Edmonton    C   0
Montreal    A   6
Montreal    B   0
Montreal    C   0
Toronto     A   13
Toronto     B   0
Toronto     C   8
Vancouver   A   0
Vancouver   B   16
Vancouver   C   0
Windsor     A   0
Windsor     B   0
Windsor     C   1
Winnipeg    A   0
Winnipeg    B   3
Winnipeg    C   0

2 个答案:

答案 0 :(得分:10)

使用stackreset_index

In [1260]: ndf.stack().reset_index()
Out[1260]:
         City Group  Sales
0    Edmonton     A      4
1    Edmonton     B      0
2    Edmonton     C      0
3    Montreal     A      6
4    Montreal     B      0
5    Montreal     C      0
6     Toronto     A     13
7     Toronto     B      0
8     Toronto     C      8
9   Vancouver     A      0
10  Vancouver     B     16
11  Vancouver     C      0
12    Windsor     A      0
13    Windsor     B      0
14    Windsor     C      1
15   Winnipeg     A      0
16   Winnipeg     B      3
17   Winnipeg     C      0

答案 1 :(得分:6)

这很容易实现:

ndf.stack().reset_index()

         City Group  Sales
0    Edmonton     A      4
1    Edmonton     B      0
2    Edmonton     C      0
3    Montreal     A      6
4    Montreal     B      0
5    Montreal     C      0
6     Toronto     A     13
7     Toronto     B      0
8     Toronto     C      8
9   Vancouver     A      0
10  Vancouver     B     16
11  Vancouver     C      0
12    Windsor     A      0
13    Windsor     B      0
14    Windsor     C      1
15   Winnipeg     A      0
16   Winnipeg     B      3
17   Winnipeg     C      0