Skip to content Skip to sidebar Skip to footer

How To Prepare Pandas Df For Venn Diagram

I have a Pandas dataframe as follows: +-----+-----------+ | ID | VALUE | +-----+-----------+ | A | Today | +-----+-----------+ | A | Yesterday | +-----+-----------+ |

Solution 1:

You can use crosstab to get the dummies, then matrix product to see cooccurrences:

s = pd.crosstab(df['ID'],df['VALUE'])

pair_intersection = s.T @ s
all_three = s.ne(0).all(1)

Then, pair_intersection looks like:

VALUE      Today  Tomorrow  Yesterday
VALUE                                
Today          3         2          2
Tomorrow       2         4          1
Yesterday      2         1          2

Then counts of two overlapping groups can be extracted using pair_intersection.at['Today', 'Tomorrow'].

all_three is

ID
A    False
B    False
C    False
D    False
E     True
dtype: bool

And thus the number of instances that fall in all three groups is sum(all_three)

Post a Comment for "How To Prepare Pandas Df For Venn Diagram"