Marco Fumagalli
Marco Fumagalli

Reputation: 2477

Making a contingency table with continous variable

i'm stuck on a problem in python. i want to create a contingency table but i have variable which is continous. suppose:

a=[1,2,5,9,10,11.3,11.9,12.5,18.8]
b=["yes","no","yes","no","yes","no","no","no","yes"]

and i want to create a contingency table that looks like

              yes   | no
[1,8]          2      1
[9,18.8]       2      4

how to do that? thanks in advance. i look over several functions in pandas, like cut and crosstab, but i am not able to reach my purpose.

Upvotes: 1

Views: 437

Answers (1)

user2285236
user2285236

Reputation:

A pandas solution would be like this:

Construct the dataframe:

df = pd.DataFrame({"a": a, "b": b})

Use pd.cut to find the intervals and pass that to crosstab:

pd.crosstab(pd.cut(df['a'], bins = [1, 8, 18.8], include_lowest=True), df['b'])
Out[41]: 
b          no  yes
a                 
[1, 8]      1    2
(8, 18.8]   4    2

Upvotes: 3

Related Questions