in Education by
So I have a list of people, each of them are given more than 2 books, 4 books are possible. I want to do a groupby and check frequency of combination of book received such as [ID, books] such ID: 1, he has Books: A, B I want to know how many people had received book combination of A and B. Technically if someone has books A,B,C; he will have combination of (A,B),(A,C),(B,C),(A,B,C). Input: df = pd.DataFrame({'user': [1, 1, 2, 2, 3, 3, 3], 'disease': ['a', 'b', 'b', 'c', 'a', 'b', 'c']})[enter image description here][1] enter image description here JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
You can use set operations. Identify users with a given target combination: target = {'a', 'b'} df.groupby('user')['disease'].agg(lambda x: target.issubset(x)) Output: user 1 True 2 False 3 True Name: disease, dtype: bool Count the number of users that match the target: target = {'a', 'b'} df.groupby('user')['disease'].agg(lambda x: target.issubset(x)).sum() Output: 2

Related questions

0 votes
    Problem: Select first N rows and last row in Python Pandas. only gets 1 row which is index 9 and ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 7, 2022 in Education by JackTerrance
0 votes
    I have some columns that have the same names. I would like to add a 1 to the repeating column ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 6, 2022 in Education by JackTerrance
0 votes
    In the code below I pivot a dataframe using an index date. After the pivot, I need to get the ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 5, 2022 in Education by JackTerrance
0 votes
    I'm starting with input data like this df1 = pandas.DataFrame( { "Name" : ["Alice", "Bob", "Mallory", ... Any hints would be welcome. Select the correct answer from above options...
asked Jan 28, 2022 in Education by JackTerrance
0 votes
    I have the pandas data frame with a column designated to town names. After each town name, I am adding a word " ... .csv', index=False) Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    I have the pandas data frame with the column designated to town names. After each town name, I am adding a word ... .csv', index=False) Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    I have a simple method to search a pandas dataframe column for a list of keywords; however, I'd like to create a ... do everyth 28,passei o dia com o meu amor comemo demai...
asked Apr 13, 2022 in Education by JackTerrance
0 votes
    I have a 20 x 4000 dataframe in python using pandas. Two of these columns are named Year and quarter. I'd ... anyone help with that? Select the correct answer from above options...
asked Jan 28, 2022 in Education by JackTerrance
0 votes
    In a certain city, 5% of all the persons in town have unlisted phone numbers. If you select 100 names at ... directory, how many people selected will have unlisted phone numbers?...
asked Feb 12, 2021 in Education by JackTerrance
0 votes
    I have one Dataframe. Dataframe1: desc id result A 1 Yes A 2 No A 3 Yes A 4 No B 1 No ... questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 11, 2022 in Education by JackTerrance
0 votes
    I have one Dataframe. Dataframe1: desc id result A 1 Yes A 2 No A 3 Yes A 4 No B 1 No ... questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    I have one Dataframe. Dataframe1: desc id result A 1 Yes A 2 No A 3 Yes A 4 No B 1 No ... questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    I am trying to identify the location of stops from gps data but need to account for some gps drift. I have identified stops and ... ] = (df.groupby('DAY')['LAT'].diff().abs()...
asked Apr 21, 2022 in Education by JackTerrance
0 votes
    I am trying to identify the location of stops from gps data but need to account for some gps drift. I have identified stops and ... ] = (df.groupby('DAY')['LAT'].diff().abs()...
asked Apr 20, 2022 in Education by JackTerrance
0 votes
    I am trying to groupby a column and compute value counts on another column. import pandas as pd dftest = pd. ... Amt, already exists Select the correct answer from above options...
asked Feb 1, 2022 in Education by JackTerrance
...