Home > Blockchain >  Use matplotlib to graph a csv column and occurrences
Use matplotlib to graph a csv column and occurrences

Time:03-14

I've looked around for sources on how to use matplotlib to create graphs (line, bar, and pie) from a csv file, but they're not exactly what I'm looking for. I am wondering how, from this example file I created below, would I graph just the Favorite Color column along with the number of occurrences of each color?

ID     Name       Favorite Color
1      Mary       Blue
2      Bob        Green
3      Simon      Red
4      Lily       Red
5      Gerald     Blue
6      Kathy      Blue

Hope that makes sense! Thank you in advance!

CodePudding user response:

The easiest way to plot this data is probably to use Pandas to load the file into a dataframe, then get the occurrences of each color using value_counts and plot the results.

import pandas as pd

df = pd.read_csv('/path/to/file.csv')
df['Favorite Color'].value_counts().plot.bar()
  • Related