Issue
Given data like down below
test = pd.DataFrame(np.array([
["Yes", "No", "No", "Yes", "Maybe"],
["Yes", "Yes", "Yes", "Maybe", "Maybe"],
["Maybe", "Yes", "Yes", "No", "No"],
["No", "Yes", "Maybe", "No", "No"],
["Maybe", "Maybe", "Maybe", "No", "Yes"],
])
,columns=['a','b','c','d', 'e'])
I want something that looks like image down below.
So column a
would have bar graph that has three bar each indicating count of 2 "Yes", 1 "No", 2 "Maybe". And so does the columns remaining.
I tried making new dataframe temp = df.apply(pd.Series.value_counts)
and with sns.countplot()
but It resulted just 5 bar instead of 3*5 bar.
Please help me to make bar graph that has multilabel from multiple column.
Thank you for reading.
Solution
Use melt
:
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
data = {'a': ['Yes', 'Yes', 'Maybe', 'No', 'Maybe'],
'b': ['No', 'Yes', 'Yes', 'Yes', 'Maybe'],
'c': ['No', 'Yes', 'Yes', 'Maybe', 'Maybe'],
'd': ['Yes', 'Maybe', 'No', 'No', 'No'],
'e': ['Maybe', 'Maybe', 'No', 'No', 'Yes']}
df = pd.DataFrame(data)
df1 = df.melt(var_name='Additional service', value_name='Has service')
sns.countplot(x='Additional service', hue='Has service', data=df1)
plt.show()
Answered By - Corralien
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.