9

Let us say I have the following dataframe:

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)

The result looks like this:

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

How can I flatten it such that it becomes a single level data frame but with a column specifying the group?

I.e., the result should be:

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 Answers 2

16

Use stack and reset_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
Sign up to request clarification or add additional context in comments.

2 Comments

I think its time to start a new session =p
Don't do it... keep the faith
7

This is easily accomplished as such:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.