Question
Given the table below is dataset, There are 4 columns and first 14 rows of the dataset is given but there are over 10,000 rows.
Given the table below is dataset, There are 4 columns and first 14 rows of the dataset is given but there are over 10,000 rows. In a order there are multiple products being sold to a client from different industry
I want to find out, How frequency of product pair orders vary accross industry
In other words, I want the pairs that appear the most accross all orders and by industry. For example in Order 4997 (Cat 1, Cat 2, IT) - 1 (Cat 2, Cat 3, IT) - 1 and (Cat 1, Cat 3, IT ) - 1. In order 2001, (Cat 1, Cat 2, Real estate) - 1 (Cat1, Cat 3, Real estate) - 1 (Cat 2, Cat 3, Real estate) - 1 appear once In total, I want to get frequency of all order pairs based on the industry
Note - there are only 3 categories and product ID is unique column, Order ID and Industry has 3 unique values i.e IT, finance and Real estate
OrderID ProdID Prodcategory Client ID 4997 1 Cat 1 IT 4997 2 Cat 1 IT 4997 3 Cat 2 IT 4997 4 Cat 3 IT 2001 5 Cat 1 Real Est 2001 6 Cat 2 Real Est 2001 7 Cat 2 Real Est 2001 8 Cat 2 Real Est 2001 9 Cat 3 Real Est 2376 10 Cat 3 Finace 2376 11 Cat 1 Finance 2376 12 Cat 2 Finance 2376 13 Cat 3 Finance 2376 14 Cat 1 Finance
MY approach ``` from itertools import combinations cats=df.groupby('OrderID')['Prodcategory']\ .agg(list)\ .apply(lambda x:list(combinations(set(x),2)))\ .explode()
cats OrderID 2001 (Cat2, Cat1) 2001 (Cat2, Cat3) 2001 (Cat1, Cat3) 2376 (Cat2, Cat1) 2376 (Cat2, Cat3) 2376 (Cat1, Cat3) 4997 (Cat2, Cat1) 4997 (Cat2, Cat3) 4997 (Cat1, Cat3)
cats.value_counts()
(Cat2, Cat3) 3 (Cat1, Cat3) 3 (Cat2, Cat1) 3 ```
this gives me count of category pairs accross all orders, I have to include Industry in this code too but I am confused how to.
Expected output is something like
(Cat2, Cat3) IT 10 (Cat2, Cat3) real estate 5 (Cat1, Cat3) finance 2 (Cat2, Cat1) It 3
and so on
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started