简体   繁体   中英

Seaborn Catplot similar to Pandas groupby

I am working with the Portuguese Bank Marketing dataset http://archive.ics.uci.edu/ml/datasets/Bank+Marketing#

I would like to visualise the conversion rate per some categorical feature eg occupation or marital status.

Using pandas groupby() as show below

df.groupby(["marital","y"])["y"].count().plot(kind="bar")

I obtain the following graph在此处输入图片说明

However, I would like to create a more readable graph, similar to the ones in seaborn tutorials. Where X is some categorical feature, Y is some value and the Hue groups them per some other metric. 在此处输入图片说明

My attempts so far result in the following errors:

sns.catplot(x = df["job"].value_counts().index, 
            y = df["job"].value_counts().values, 
            hue="y", 
            data=df, 
            kind="bar")

ValueError: Grouper and axis must be same length

Any pointers will be appreciated!

Is it what you want:

df = sns.load_dataset('titanic')

df.groupby(['sex', 'class']).size().unstack().plot.bar()

# also
# pd.crosstab(df['sex'], df['class']).plot.bar()

Output:

在此处输入图片说明

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