mytabi
mytabi

Reputation: 779

pandas : reverse of a crosstab

I have a data frame like the following enter image description here

I have a code to do crosstab

print(pd.crosstab(mypd['a'],mypd['b'],mypd['c']))

and gives enter image description here

what's the revers of crosstab , and maybe give back column a,b and c ?

Upvotes: 2

Views: 937

Answers (1)

jezrael
jezrael

Reputation: 862701

Use DataFrame.stack with Index.repeat by counts and then MultiIndex.to_frame:

df = pd.crosstab(mypd['a'], [mypd['b'],mypd['c']])

s = df.stack([0,1])

new = s.index.repeat(s).to_frame(index=False)

Sample:

print (mypd)
     a    b      c  d
0  foo  one   dull  1
1  foo  one   dull  2
2  foo  one  shiny  3


df = pd.crosstab(mypd['a'], [mypd['b'],mypd['c']])
print (df)
b    one      
c   dull shiny
a             
foo    2     1

s = df.stack([0,1])
print (s)
a    b    c    
foo  one  dull     2
          shiny    1
dtype: int64

new = s.index.repeat(s).to_frame(index=False)
print (new)
     a    b      c
0  foo  one   dull
1  foo  one   dull
2  foo  one  shiny

Upvotes: 3

Related Questions