pandas groupby, split df and rename multiple sheets

I have a dataframe like as below import numpy as np import pandas as pd from numpy.random import default_rng rng = default_rng(100) cdf = pd.DataFrame({‘Id’:[1,2,3,4,5], ‘customer’: rng.choice(list(‘ACD’),size=(5)), ‘segment’: rng.choice(list(‘PQRS’),size=(5)), ‘manager’: rng.choice(list(‘QWER’),size=(5)), ‘dumma’: rng.choice((1234),size=(5)), ‘damma’: rng.choice((1234),size=(5)) }) I would like to do the below a) create an excel file output with multiple sheets (based on segment… Read More pandas groupby, split df and rename multiple sheets

Pandas groupby columns and multiply two other columns in the aggregate function

I have a hopefully easy problem for some help stack helpers! I have a dataframe: df = pd.DataFrame({‘Quantity’: [2, 3, 4, 1, 2, 1, 4, 5], ‘User’: [‘A’, ‘A’, ‘B’, ‘B’, ‘B’, ‘C’, ‘C’, ‘C’], ‘Price’: [5, 3, 2, 6, 2, 3, 4, 5], ‘Shop’: [‘X’, ‘X’, ‘X’, ‘Y’, ‘Z’, ‘Z’, ‘X’, ‘Y’], ‘Day’: [‘M’,… Read More Pandas groupby columns and multiply two other columns in the aggregate function

SQL: Need help to group by

this is my table: Start Stop City 2022-01-01 2022-02-15 Rom 2022-02-16 2022-03-31 Rom 2022-04-01 2022-05-10 London 2022-05-11 2022-06-11 London 2022-06-12 2022-07-10 Paris 2022-07-11 2022-08-10 Rom I like to get this result: Start Stop City 2022-01-01 2022-03-31 Rom 2022-04-01 2022-06-11 London 2022-06-12 2022-07-10 Paris 2022-07-11 2022-08-10 Rom If i use: SELECT City, MIN(Start) as STA, MAX(Stop)… Read More SQL: Need help to group by

pandas – create customer movement matrix

I have a dataframe that looks like below customer_id,month,Group A1,Jan,Premium A2,Jan,Lost A3,Jan,Lost A4,Jan,Lost A5,Jan,Loyal A6,Jan,Need Attention A1,Mar,Premium A2,Mar,Need Attention A3,Mar,Lost A4,Mar,Need Attention A5,Mar,Loyal A6,Mar,Lost t1 = pd.read_clipboard(sep=’,’) I would like to do the below a) Create a matrix against Jan and Mar month b) Fill the matrix with customer count under each group I expect… Read More pandas – create customer movement matrix

pandas: add a grouping variable to clusters of rows that meet a criteria

I have this dataframe: df = pd.DataFrame({‘forms_a_cluster’: [False, False, True, True, True, False, False, False, True, True, False, True, True, True, False], ‘cluster_number’:[False, False, 1, 1, 1, False, False, False, 2, 2, False, 3, 3, 3, False]}) The idea is that I have some criteria which, when certain rows have met it, selects those cases… Read More pandas: add a grouping variable to clusters of rows that meet a criteria

Pandas – split one row value and merge with multiple rows

I have two dataframes like as below proj_df = pd.DataFrame({‘reg_id’:[1,2,3,4,5,6,7], ‘partner’: [‘ABC_123′,’ABC_123′,’ABC_123′,’ABC_123′,’ABC_123′,’ABC_123′,’ABC_123’], ‘part_no’:[‘P123′,’P123′,’P123′,’P123′,’P123′,’P123′,’P123’], ‘cust_info’:[‘Apple’,’Apple’,’Apple’,’Apple’,’Apple’,’Apple’,’Tesla’], ‘qty_1st_year’:[100,100,600,150,50,0,10]}) order_df = pd.DataFrame({‘partner’: [‘ABC_123′,’ABC_123′,’JKL_123′,’MNO_123’], ‘part_no’:[‘P123′,’P123′,’Q123′,’P567’], ‘cust_info’:[‘Apple’,’Hyundai’,’REON’,’Renault’], ‘order_qty’:[1000,600,50,0]}) I would like to do the below a) Merge two dataframes based on partner,part_no,cust_info b) split the order_qty column from order_df and assign the appropriate portion to a new column called assigned_qty c)… Read More Pandas – split one row value and merge with multiple rows