1

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 e.g. occupation or marital status.

Using pandas groupby() as show below

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

I obtain the following graph enter image description here

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. enter image description here

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!

Michal B.
  • 111
  • 1
  • 12

1 Answers1

1

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:

enter image description here

Quang Hoang
  • 146,074
  • 10
  • 56
  • 74
  • Not necessarily. Both have very similar syntax `sns.catplot(x="sex", y="survived", hue="class", kind="bar", data=titanic)` As per https://seaborn.pydata.org/tutorial/categorical.html Regardless whether I use sns.barplot or sns.catplot(kind = "bar"), I cannot produce the graph! – Michal B. Nov 06 '20 at 14:56
  • is it `df.groupby(["marital","y"])["y"].count().unstack().plot(kind="bar")` – Quang Hoang Nov 06 '20 at 14:59
  • It is precisely that!!!! Thank you very much Quang! However, I am still wondering how to do so using Seaborn, as lets be honest, it looks much much nicer than pandas plotting! Nonetheless, this is precisely what I was looking for! I was unaware of the unstack() method! – Michal B. Nov 06 '20 at 15:03