Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
2.7 kB
1
Indexable
Never
grouped_df = df.groupby(['transaction_date', 'label']).agg({
    'total_monthly_ton_dil': 'sum',
    'MO_dealer_monthly_ton_GZPN': 'sum',
    'KP_dealer_monthly_ton_GZPN': 'sum',
    'MO_KP_dealer_monthly_ton_GZPN': 'sum',
    'client_inn_dil': 'count'
}).reset_index()

print(grouped_df.info())
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 48 entries, 0 to 47
Data columns (total 8 columns):
 #   Column                         Non-Null Count  Dtype         
---  ------                         --------------  -----         
 0   transaction_date               48 non-null     datetime64[ns]
 1   label                          48 non-null     object        
 2   total_monthly_ton_dil          48 non-null     float64       
 3   MO_dealer_monthly_ton_GZPN     48 non-null     float64       
 4   KP_dealer_monthly_ton_GZPN     48 non-null     float64       
 5   MO_KP_dealer_monthly_ton_GZPN  48 non-null     float64       
 6   client_inn_dil                 48 non-null     int64         
 7   color                          48 non-null     object        
dtypes: datetime64[ns](1), float64(4), int64(1), object(2)

grouped_df[grouped_df['label']=='dil_MO+KP']

	transaction_date	label	total_monthly_ton_dil	MO_dealer_monthly_ton_GZPN	KP_dealer_monthly_ton_GZPN	MO_KP_dealer_monthly_ton_GZPN	client_inn_dil	color
2	2023-01-01	dil_MO+KP	22254.615859	0.0	0.0	127292.46	13	red
6	2023-02-01	dil_MO+KP	19510.347573	0.0	0.0	118775.70	14	red
10	2023-03-01	dil_MO+KP	27612.267073	0.0	0.0	153347.45	12	red
14	2023-04-01	dil_MO+KP	26877.229672	0.0	0.0	188694.56	17	red
18	2023-05-01	dil_MO+KP	28811.344934	0.0	0.0	144392.96	18	red
22	2023-06-01	dil_MO+KP	4663.258970	0.0	0.0	92014.46	16	red
26	2023-07-01	dil_MO+KP	5537.268520	0.0	0.0	176764.37	15	red
30	2023-08-01	dil_MO+KP	4826.702861	0.0	0.0	131540.36	14	red
34	2023-09-01	dil_MO+KP	1594.082342	0.0	0.0	140163.36	12	red
38	2023-10-01	dil_MO+KP	6375.978418	0.0	0.0	128058.46	15	red
42	2023-11-01	dil_MO+KP	14768.483389	0.0	0.0	154676.25	18	red



unique_clients_per_label = df.groupby(['transaction_date', 'label'])['client_inn_dil'].nunique().reset_index()
unique_clients_per_label.pivot(index='transaction_date', columns='label', values='client_inn_dil')

label	dil_KP	dil_MO	dil_MO+KP	Прочие
transaction_date				
2023-01-01	153	13	8	5392
2023-02-01	161	22	9	5527
2023-03-01	161	24	7	5775
2023-04-01	191	20	10	5958
2023-05-01	191	22	10	6053
2023-06-01	188	26	10	6228
2023-07-01	200	25	9	6356
2023-08-01	202	26	8	6520
2023-09-01	219	22	9	6708
2023-10-01	208	30	9	6749
2023-11-01	212	23	12	6761
2023-12-01	213	22	10	6831
46	2023-12-01	dil_MO+KP	5421.829860	0.0	0.0	106683.98	14	red
Leave a Comment