Home > Enterprise >  Plot bar chart from imported data from csv
Plot bar chart from imported data from csv

Time:08-21

My current goal is to print a bar chart from CSV file into xlabel"Offices" and ylabel"Count of people". The problem is I'm not sure how I can calculate how many people are from respective office, which then could be translated to a bar chart with a calculated amount.

Case:

I have created a dummy CSV file, with: FirstName, LastName, DOB, PhoneNumber, Position, Employment, and Office.

Data would look like this:

  • Tom, Belington, 1999-01-01, 169874654, Manager, True, LV
  • Tom, Belington, 1999-01-01, 169874654, Agent, True, PA
  • Tom, Belington, 1999-01-01, 169874654, Agent, True, CA
  • Tom, Belington, 1999-01-01, 169874654, Agent, True, LV
  • Tom, Belington, 1999-01-01, 169874654, Agent, True, CA
  • Tom, Belington, 1999-01-01, 169874654, Agent, True, CA
  • Tom, Belington, 1999-01-01, 169874654, Agent, True, PA

I'm currently using pandas and matplotlib

CodePudding user response:

This can be more easily done with sns.countplot:

import seaborn as sns
sns.countplot(df.Office)

Another way is to get count per office with value_counts:

df.Office.value_counts().plot(kind='bar')

CodePudding user response:

You can use seaborn countplot or use value_counts() and then bar plot if you want to use matplotlib only.

Seaborn Code

df=pd.read_csv('inputfile.csv')
ax=sns.countplot(data=df, x='Office')
ax.set(xlabel='Offices', ylabel='Count of people')

enter image description here

Using matplotlib only

df=pd.read_csv('abc.csv')
df.Office.value_counts().plot.bar()
ax.set(xlabel='Offices', ylabel='Count of people')

enter image description here

  • Related