piRSquared
piRSquared

Reputation: 294318

Reverse a Cross Tabulation or Frequency Table

Suppose I have a frequency table df defined as:

dat = [[0, 2, 1], [1, 0, 3], [4, 1, 1]]
idx = pd.Index([*'abc'], name='One')
col = pd.Index([*'xyz'], name='Two')
df = pd.DataFrame(dat, idx, col)

df

Two  x  y  z
One         
a    0  2  1
b    1  0  3
c    4  1  1

How do I "invert" this to get a dataframe pre_df

   One Two
0    a   y
1    a   y
2    a   z
3    b   x
4    b   z
5    b   z
6    b   z
7    c   x
8    c   x
9    c   x
10   c   x
11   c   y
12   c   z

Such that pd.crosstab(pre_df.One, pre_df.Two) would get me back to df

Two  x  y  z
One         
a    0  2  1
b    1  0  3
c    4  1  1

Upvotes: 3

Views: 738

Answers (1)

Quang Hoang
Quang Hoang

Reputation: 150745

Try stack and repeat:

s = df.stack()
s.index.repeat(s).to_frame().reset_index(drop=True)

Output:

   One Two
0    a   y
1    a   y
2    a   z
3    b   x
4    b   z
5    b   z
6    b   z
7    c   x
8    c   x
9    c   x
10   c   x
11   c   y
12   c   z

Upvotes: 3

Related Questions