Reputation: 479
I'm sure this has been answered somewhere, but I'm not great with pandas and need someone to break it down for me.
I have this function:
def process_data(data):
data = data[data['Bucket Number'] == 25.0].groupby(['Activity Month', 'Agent Sign']).agg({'Total Ping Current Forecast': [np.sum]})
return data
EDIT: Fixed Sample
That produces this output:
Total Ping Current Forecast
sum
Activity Month Agent Sign
202001 CRCDIF 618485.0
CRCTLD 47746137.0
DFW1DF 16220228.0
HDQ9LO 19995.0
HDQBDE 10739.0
... ...
202007 HDQRPT 0.0
HDQZED 0.0
MSCUHD 0.0
RDUSMD 56732.0
{Various} 245079251.0
How would I make a bar plot where the X-axis is the Activity Month
, the Y-axis is the sum, and for each tick is the two largest Agent Sign
bars?
So for example the tick at 202001
would have two bars, one for Agent Sign
CRCTLD and one for DFW1DF.
Upvotes: 0
Views: 335
Reputation: 150815
Let's try extracting the rows with groupby().cumcount
(also possible with nlargest
), then plot with sns.barplot()
:
processed = process_data(data)
sns.barplot(data=processed.sort_values('sum',ascending=False)
.assign(rank=lambda x: x.groupby(level=0).cumcount())
.reset_index()
.loc[lambda x: x['rank']<=1],
x='Activity Month',y='sum', hue='rank'
)
Output:
Upvotes: 1