0

Given this code

bytypecity = df.groupby(['Type', 'City']).agg(
    Avg_Size_sqm=('Size_sqm', 'mean'),
    Avg_Price_eur=('Price_eur', 'mean'),
    Avg_Price_sqm=('Price_sqm', 'mean'),
)
display(bytypecity)
typecity = bytypecity.reset_index()

sns.catplot(data=typecity, kind='bar',
            y='Avg_Price_sqm', x='Type'
            )
plt.xticks(rotation=90)
plt.show()

And its results:

                                    Avg_Size_sqm  Avg_Price_eur  Avg_Price_sqm
Type                       City                                               
Appartamento               Bologna    142.500000   4.750000e+05    3351.000000
                           Firenze    159.500000   7.738333e+05    4777.833333
                           Napoli     165.000000   1.415000e+06    7788.500000
                           Palermo    175.000000   3.800000e+05    2100.000000
                           Roma       142.500000   1.334500e+06    8393.500000
                           Torino     159.000000   4.830000e+05    3049.600000
Appartamento all'asta      Bologna    117.444444   1.961389e+05    1646.000000
                           Firenze     77.000000   1.365000e+05    1773.000000
                           Napoli      94.454545   5.665227e+04     613.636364
                           Palermo    168.000000   1.436905e+05     894.250000
                           Roma        89.500000   9.326875e+04    1166.500000
Appartamento buono stato   Napoli     140.000000   1.199900e+06    8571.000000
Appartamento ottimo stato  Napoli     166.000000   9.700000e+05    5843.000000
Attico                     Palermo    135.000000   2.400000e+05    1778.000000
                           Roma        63.500000   2.395000e+05    3780.000000
Bilocale                   Bologna     65.400000   1.231500e+05    1996.000000
                           Firenze     56.600000   2.638000e+05    4665.400000
                           Milano      66.181818   3.541818e+05    5378.545455
                           Napoli     137.500000   1.600000e+05    1545.500000
                           Palermo     72.666667   1.076667e+05    1411.666667
                           Roma        61.000000   2.596000e+05    4245.200000
                           Torino      85.000000   4.370000e+05    5141.000000
Casa indipendente all'asta Bologna    137.000000   2.475000e+05    1807.000000
Monolocale                 Milano      35.000000   1.950000e+05    5571.000000
                           Napoli      45.000000   2.200000e+05    4889.000000
                           Palermo     20.000000   3.400000e+04    1700.000000
                           Torino      30.000000   3.900000e+04    1300.000000
Palazzo - Edificio         Firenze    850.000000   2.850000e+06    3353.000000
Palazzo - stabile all'asta Milano      54.666667   8.150000e+04    1463.333333
Quadrilocale               Bologna    109.333333   3.391667e+05    3171.333333
                           Firenze     89.166667   3.561667e+05    4053.333333
                           Milano     146.000000   6.050000e+05    4144.000000
                           Napoli      92.500000   2.775000e+05    2956.000000
                           Palermo    124.333333   1.306667e+05    1064.000000
                           Roma       110.000000   2.900000e+05    2636.000000
                           Torino     101.666667   3.140000e+05    3111.000000
ETC...

enter image description here

I have two questions:

  1. How can I sort Type in the sns.catplot by Avg_Price_sqm from the highest to the lowest?
  2. How can I access the groupby Multiindex indexes as separate lists? bytypecity.index returns them in an aggregated form.
antobzzll
  • 51
  • 5

1 Answers1

0

You can pass order = typecity.Avg_Price_sqm.sort_values(ascending=False).index to sns.catplot. Also, you can get multiindexes as a list with bytypecity.index.tolist()

The code at the end would look like the following:

order = typecity.Avg_Price_sqm.sort_values(ascending=False).index

sns.catplot(data=typecity, kind='bar',
            y='Avg_Price_sqm', x='Type'
            )
Marioanzas
  • 1,663
  • 2
  • 10
  • 33
Nuri Taş
  • 3,828
  • 2
  • 4
  • 22
  • It doesn't work, it return the full list of index. I need to sort the grouped columns as in groupby. – antobzzll Aug 16 '22 at 09:20
  • `bytypecity.index.tolist()` return a list of tuples. How do i retrieve only the list of the first index column? – antobzzll Aug 16 '22 at 09:21
  • Is there also a way to use that catplot directly on `bytypecity` groupby? – antobzzll Aug 16 '22 at 09:22
  • If you want only the first index of multiindexes, you may use `df.index.droplevel(1).tolist()`. Here `df` is the dataframe with multiindexes – Nuri Taş Aug 16 '22 at 09:23