简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM