0

I have a dataframe of size (3,100) that is filled with some random float values. Here is a sample of how the data frame looks like

     A         B         C
  4.394966  0.580573  2.293824    
  3.136197  2.227557  1.306508    
  4.010782  0.062342  3.629226
  2.687100  1.050942  3.143727
  1.280550  3.328417  2.247764
  4.417837  3.236766  2.970697
  1.036879  1.477697  4.029579
  2.759076  4.753388  3.222587
  1.989020  4.161404  1.073335
  1.054660  1.427896  2.066219
  0.301078  2.763342  4.166691
  2.323838  0.791260  0.050898
  3.544557  3.715050  4.196454
  0.128322  3.803740  2.117179
  0.549832  1.597547  4.288621

This is how I created it

df = pd.DataFrame(np.random.uniform(0,5,size=(100, 3)), columns=list('ABC'))

Note: pd is pandas

I want to plot a bar chart that would have three segments in x-axis where each segment would have 2 bars. One would show number of values less than 2 and other greater than equal to 2.

So on x-axis there would be two bars attached for column A, one with total number of values less than 2 and one with greater than equal to 2, and same for B and C

Can anyone suggest anything? I was thinking of using seaborn and setting hue value for differentiating two classes (less than 2 and greater than equal to 2) but then again hue attribute only works for categorical value and I can only set one column in x-axis attribute.

Any tips would be appreciated.

1 Answer 1

1

You must use a filter and then count them, then you must use plot(kind='bar')

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

df = pd.DataFrame(np.random.uniform(0,5,size=(100, 3)), columns=list('ABC'))

dfout = pd.DataFrame({'minor' : df[df<= 2].count(),
     'major' : df[df > 2].count() })

dfout.plot(kind='bar')
plt.show()

enter image description here

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

2 Comments

I don't know why someone voted down, but this worked so i am voting up and taking this as an accepted answer
If you voted negative you should post your motive to be able to improve the post.

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.