Yilmaz
Yilmaz

Reputation: 119

Plot Multiple boolean columns in X - axis in Python

I have a data frame (df3) with features like: These are the details of customers who accepted the marketing campaigns. 1 means accepted and 0 means not accepted

Customer ID AcceptedCmp1 AcceptedCmp2 AcceptedCmp3 Income Kidhome
123             1            0            0        3456   1 
456             0            1            0        5678   0 
786             1            1            1        6555   1
987             0            0            0        5444   1

I want the output to be something like this showing the marketing campaigns with highest and lowest acceptance:

Thanks enter image description here

Upvotes: 0

Views: 2440

Answers (2)

Prakash Dahal
Prakash Dahal

Reputation: 4875

You can use this. Countplot of seaborn will count the values of columns and plot it, use pd.melt to plot columns. Here x="variable" and hue="value" came from melted(pd.melt(df3)) df3.

import seaborn as sns
import pandas as pd
sns.countplot(x="variable", hue="value", data=pd.melt(df3))

enter image description here

Upvotes: 2

Mohil Patel
Mohil Patel

Reputation: 510

I think this is what you are looking for

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

zeros = [data[column].value_counts()[0] for column in data.columns]
ones = [data[column].value_counts()[1] for column in data.columns]
n = len(data.columns)
plt.bar(np.arange(n)-0.1, zeros, width=0.2, label='0')
plt.bar(np.arange(n)+0.1, ones, width=0.2, label='1')
plt.xticks(np.arange(n), data.columns)
plt.legend()
plt.show()

enter image description here

Upvotes: 1

Related Questions