简体   繁体   中英

How to plot Pandas .groupby() objects in Seaborn - using method chaining

I would be really grateful for any advice on how to pipe a Pandas .groupby() object into Seaborn

Im trying to plot the .groupby() object but it give me an error message: ValueError: If using all scalar values, you must pass an index

gapminder.\
    groupby('year').\
    agg({'pop' : ['sum'],
         'lifeExp' : ['mean']}).\
    reset_index().\
    pipe((sns.relplot, "data"), x = "pop", y = "lifeExp", kind = "scatter").\
    set(xscale = "log")
plt.show()

Pass a lambda, then you can call the function as you please:

.pipe(lambda d: sns.relplot(data=d, x="pop", y="lifeExp", kind="scatter"))

Where d represents your DataFrame, passed in whole as the sole argument to .pipe .

The answer is helped by a new feature added to Pandas in version 0.25, "Named aggregation with .groupby()". This allows for the creation of variables through the .agg() function and the output into a tidy dataframe with a uniform index, as opposed to the multi-indexed groupby object in Pandas <= version 0.24. Below shows how a query can be chained through Pandas and also seamlessly into Seaborn.

gapminder.\
   groupby('year').\
   agg(pop_sum = ('pop', sum), 
       lifeExp_mean = ('lifeExp', 'mean')).\
   reset_index().\
   pipe((sns.relplot,"data"), x="pop_sum", y="lifeExp_mean", kind="scatter").\
   set(xscale = "log")
plt.show()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM