plot aggregate pandas data

PHOTO EMBED

Mon Nov 29 2021 03:16:00 GMT+0000 (Coordinated Universal Time)

Saved by @rafasacaan ##pandas ##plot

def f(x):
    d={}
    d['conversion_rate'] = x.ORDER_PLACED.mean()
    d['n_orders'] = len(x)
    d['aov'] = x.TOTAL_PRICE_PTC.mean()
    return pd.Series(d, index=['conversion_rate','n_orders','aov'])

AGG_CITIES = {
    city: (df
           .query('CITY_PTC==@city')
           .groupby(['COUNTRY_CODE_PTC','CITY_PTC','FREIGHT_PERC_BIN'])
           .apply(f)
           .reset_index())
    for city in df.CITY_PTC.unique()
}

colors = plt.rcParams["axes.prop_cycle"]()
fig, axes = plt.subplots(ncols=2, nrows=2, sharex=True, sharey=True, figsize=(18, 12))

for ax, (city, vals) in zip(axes.ravel(), AGG_CITIES.items()):
    c = next(colors)["color"]
    ax.plot(vals.FREIGHT_PERC_BIN, vals.conversion_rate, color=c, alpha=0.3, label=city)
    ax.set_ylabel('CONVERSION RATE')
    ax.set_xlabel('FREIGHT_PERC_BIN')   
    ax.xaxis.set_tick_params(rotation=65)
#    ax.axvline(x=np.mean(vals), color=c, linestyle="--")
#    ax.axvline(x=0, color="black")
#    ax.set_xlim(-0.3, 0.3)
    ax.legend()
content_copyCOPY