Home > Net >  How to create pivot table from this data
How to create pivot table from this data

Time:05-20

I created a data frame with 2 columns that have random 1,0 value.

import numpy as np
import pandas as pd
x = np.random.randint(2, size=1000).reshape(500,2)
data = pd.DataFrame(x ,columns=['x','y'])
data

How can I create a table (or another data frame) that looks like this:

     0      1
0  count count
1  count count 

I think it has to be pivot table, but I can get this to work.

CodePudding user response:

If I understand you correctly, you can use pd.crosstab:

print(pd.crosstab(data.x, data.y))

Prints:

y    0    1
x          
0  132  134
1  115  119
  • Related