24

I have a parsed very large dataframe with some values like this and several columns:

Name Age Points ...
XYZ  42  32pts  ...
ABC  41  32pts  ...
DEF  32  35pts
GHI  52  35pts
JHK  72  35pts
MNU  43  42pts
LKT  32  32pts
LKI  42  42pts
JHI  42  35pts
JHP  42  42pts
XXX  42  42pts
XYY  42  35pts

I have imported numpy and matplotlib.

I need to plot a graph of the number of times the value in the column 'Points' occurs. I dont need to have any bins for the plotting. So it is more of a plot to see how many times the same score of points occurs over a large dataset.

So essentially the bar plot (or histogram, if you can call it that) should show that 32pts occurs thrice, 35pts occurs 5 times and 42pts occurs 4 times. If I can plot the values in sorted order, all the more better. I have tried df.hist() but it is not working for me. Any clues? Thanks.

5
  • use the result from df['Points'].value_counts()
    – Paul H
    Oct 21, 2014 at 0:31
  • Oh. I do that and I print the values. But not able to plot it as a graph(something like a histogram) ... that is the problem.
    – SMU
    Oct 21, 2014 at 0:50
  • Save the object and plot that.
    – Paul H
    Oct 21, 2014 at 0:51
  • Thanks. Also, can you say how I can just remove the non-numeric characters from the column? Like I want to remove the pts from the 32pts so that it doe not treat it like a string...
    – SMU
    Oct 21, 2014 at 0:55
  • That's a totally separate question. You can find a lot of resources on that type of problem on this website. Since you're new I'll throw you a bone and get you started in my answer.
    – Paul H
    Oct 21, 2014 at 1:00

2 Answers 2

50

I would plot the results of the dataframe's value_count method directly:

import matplotlib.pyplot as plt
import pandas

data = load_my_data()
fig, ax = plt.subplots()
data['Points'].value_counts().plot(ax=ax, kind='bar')

If you want to remove the string 'pnts' from all of the elements in your column, you can do something like this:

df['points_int'] = df['Points'].str.replace('pnts', '').astype(int)

That assumes they all end with 'pnts'. If it varying from line to line, you need to look into regular expressions like this: Split columns using pandas

And the official docs: http://pandas.pydata.org/pandas-docs/stable/text.html#text-string-methods

4
  • hmm...Thanks. I do have the libs installed but the plots are not showing up. Am I missing something else? Is there something else that needs to be enabled to see the plots? I am working on a VM which runs Ubuntu 14.04
    – SMU
    Oct 21, 2014 at 5:18
  • Quickest thing to do would be to use fig.savefig(<path_to_figure>) to save the figure as a file. You could also define an interactive backend to for matplotlib to use, but that has also been covered in several other questions on SO. (@SMU)
    – Paul H
    Oct 21, 2014 at 5:20
  • @PaulH I just wanted to know how to get this plot sorted on values not on frequency.
    – user6429297
    Aug 10, 2016 at 21:01
  • 1
    data['Points'].value_counts().sort_index().plot(ax=ax, kind='bar') to sort on values
    – matthiash
    Sep 6, 2021 at 16:08
14

Seaborn package has countplot function which can be made use of to make frequency plot:

import seaborn as sns

ax = sns.countplot(x="Points",data=df)
1
  • You should indent the code four times (the second line). Then that line will display as code. Also, you can surround the word "countplot" with ` . Then it will appear as "... has countplot function...". Sep 20, 2019 at 4:12

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.