Afternoon All,
I have been trying to resolve this for awhile, any help would be appreciated.
Here is my dataframe:
Channel state rfq_qty
A Done 10
B Tied Done 10
C Done 10
C Done 10
C Done 10
C Tied Done 10
B Done 10
B Done 10
I would like to:
- Group by channel, then state
- Sum the rfq_qty for each channel
- Count the occurences of each 'done' string in state ('Done' is treated the same as 'Tied Done' i.e. anything with 'done' in it)
- Display the channels rfq_qty as a percentage of the total number of rfq_qty (80)
Channel state rfq_qty Percentage
A 1 10 0.125
B 3 30 0.375
C 4 40 0.5
I have attempted this with the following:
df_Done = df[
(
df['state']=='Done'
)
|
(
df['state'] == 'Tied Done'
)
][['Channel','state','rfq_qty']]
df_Done['Percentage_Qty']= df_Done['rfq_qty']/df_Done['rfq_qty'].sum()
df_Done['Done_Trades']= df_Done['state'].count()
display(
df_Done[
(df_Done['Channel'] != 0)
].groupby(['Channel'])['Channel','Count of Done','rfq_qty','Percentage_Qty'].sum().sort_values(['rfq_qty'], ascending=False)
)
Works but looks convoluted. Any improvements?