I need to derive the above expected df3 by merging df & df1 and below statistics is required :
Pointed to be noted while merging : If value is not there in "Desc1" it should pick the value from "Desc2"
- Each Card name and cross tab each category and percentage of amount spent on each category. ( i.e ) Sum of amount of each category/total sum of amount for each card name
- Top 2 categories of each Card name based on the Amount spent ( All this this should be grouped by Card ) Can help pls ? Also suggest what more statistics can we infer from df3 ?
I have 2 DFs as below :
df = pd.DataFrame({"Customer_no": ['1', '1', '1', '2', '2', '6', '7','8','9','10'],
"Card_no": ['111', '222', '333', '444', '555', '666', '777','888','999','000'],
"Card_name":['AAA','AAA','BBB','CCC','AAA','DDD','EEE','BBB','CCC','CCC'],
"Group_code":['123','123','456','678','123','434','678','365','678','987'],
"Amount":['100','240','450','212','432','123','543','567','232','453']})
Second DF :
df1 = pd.DataFrame({"Group_code": ['123', '123','456', '678','678', '434', '987','421'],
"Desc1": ['Electrical', 'Electrical','Hardware', 'House', 'House', 'Car','','Toy'],
"Desc2":['Electricals111','Electricals123','Hardware112','House232','House112',
'Car','Bike','Toy']})
Expected DF :
df3 = pd.DataFrame({"Customer_no": ['1', '1', '1', '2', '2', '6', '7','8','9','10'],
"Card_no": ['111', '222', '333', '444', '555', '666', '777','888','999','000'],
"Card_name":['AAA','AAA','BBB','CCC','AAA','DDD','EEE','BBB','CCC','CCC'],
"Group_code":['123','123','456','678','123','434','678','365','678','987'],
"Amount":['100','240','450','212','432','123','543','567','232','453'],
"Category" :['Electrical','Electrical','Hardware','House','Electrical','Car','House','','House','Bike']})