0

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.

enter image description here

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.

1 Answer 1

2

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()

melt_countplot

Sign up to request clarification or add additional context in comments.

1 Comment

So I didn't need to use value count since count plot itself sum it up for me... How dumb I am... Thanks!!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.