0

So I want to generate a chart graph from a csv data file, and I've been following a guide but I can't seem to manipulate my code in such a way to get what I want.

So here is what I have so far:

from pandas import DataFrame, read_csv
import matplotlib.pyplot as plt
import pandas as pd 
import sys 
import matplotlib 
df = pd.read_csv("TB_burden_countries_2018-03-06.csv")
df = df.set_index(['country'])
df2 = df.loc["Zimbabwe", "e_mort_num"]
df2 = df.loc["Zimbabwe", "e_mort_num"]
df = pd.DataFrame(data = df2, columns= ["e_mort_num"])
df.columns = ["Mortality"]
print(df2)

This code was just so I can choose a specific country (Zimbabwe) and look at its population number (e_mort_num). What could I write to generate a chart graph? I've been using this tutorial : http://pbpython.com/simple-graphing-pandas.html, but I'm having trouble manipulating variable names, a I'm not too sure what I should be doing. If you require more information, please say so. Thank you for your help!

CSV bit of interest:

  Country   Year    Mortality
    Zimbabwe    2000    20000
    Zimbabwe    2001    18000
    Zimbabwe    2002    17000
    Zimbabwe    2003    19000
    Zimbabwe    2004    19000
    Zimbabwe    2005    22000
    Zimbabwe    2006    24000
    Zimbabwe    2007    24000
    Zimbabwe    2008    23000
    Zimbabwe    2009    17000
    Zimbabwe    2010    13000
    Zimbabwe    2011    14000
    Zimbabwe    2012    14000
    Zimbabwe    2013    11000
    Zimbabwe    2014    11000
    Zimbabwe    2015    9000
    Zimbabwe    2016    5600
  • Could you post the output of `df2.head(10)` and `df.head(10)`, and clarify whether by "chart graph" you mean a bar plot? – sacuL Mar 10 '18 at 02:54
  • What kind of graph? – cs95 Mar 10 '18 at 02:54
  • Hi there, yep I mean a bar plot. Regarding the output of df2(10) or df(10) its actually too big to post ehre ([10 rows x 70 columns]), since I'm analysing world health organisation data, but let me post the other data from the above code –  Mar 10 '18 at 02:59
  • `country Zimbabwe 20000 Zimbabwe 18000 Zimbabwe 17000 Zimbabwe 19000 Zimbabwe 19000 Zimbabwe 22000 Zimbabwe 24000 Zimbabwe 24000 Zimbabwe 23000 Zimbabwe 17000 Zimbabwe 13000 Zimbabwe 14000 Zimbabwe 14000 Zimbabwe 11000 Zimbabwe 11000 Zimbabwe 9000 Zimbabwe 5600 Name: e_mort_num, dtype: int64` –  Mar 10 '18 at 02:59
  • Sorry about that. The x-values are years, so from 2000 to 2016. –  Mar 10 '18 at 03:11
  • Please either provide a few rows of your .csv file or do a `df.head()` and copy and past the result by [editing](https://stackoverflow.com/posts/49204861/edit) your post, don't paste it in the comment. Without those, your question is not answerable. – hcheung Mar 10 '18 at 03:11
  • Hi, thank you for that, sorry for the ambiguity, I wasn't too sure what I should be posting. So I've chucked up the relevant bits, and regarding the df.head(), it spits out the entire dataset, which is way too big. Thanks –  Mar 10 '18 at 03:19

1 Answers1

1

Assuming your dataframe looks like this:

>>> df
     Country  Year  Mortality
0   Zimbabwe  2000      20000
1   Zimbabwe  2001      18000
2   Zimbabwe  2002      17000
3   Zimbabwe  2003      19000
4   Zimbabwe  2004      19000
5   Zimbabwe  2005      22000
6   Zimbabwe  2006      24000
7   Zimbabwe  2007      24000
8   Zimbabwe  2008      23000
9   Zimbabwe  2009      17000
10  Zimbabwe  2010      13000
11  Zimbabwe  2011      14000
12  Zimbabwe  2012      14000
13  Zimbabwe  2013      11000
14  Zimbabwe  2014      11000
15  Zimbabwe  2015       9000
16  Zimbabwe  2016       5600

You can obtain a barplot by using the following code:

# Plot mortality per year:
plt.bar(df['Year'], df['Mortality'])
# Set plot title
plt.title('Zimbabwe')
# Set the "xticks", for barplots, this is the labels on your x axis 
plt.xticks(df['Year'], rotation=90)
# Set the name of the x axis
plt.xlabel('Year')
# Set the name of the y axis
plt.ylabel('Mortality')
# tight_layout makes it nicer for reading and saving
plt.tight_layout()
# Show your plot
plt.show()

Which gives you this:

enter image description here

sacuL
  • 49,704
  • 8
  • 81
  • 106
  • Wow, that's amazing, exactly what I was looking for! Thank you so much for that sacul, very appreciated! –  Mar 10 '18 at 03:23
  • You're very welcome! In future posts, if you post a minimal example (as you did in the end) so that people can play with it, it helps a lot. – sacuL Mar 10 '18 at 03:25