-1
Country,World_Bank_Income_Level,Region,2017,2016,2015,2014,2013,2012,2011,2010,2009,2008,2007,2006,2005,2004,2003,2002,2001,2000,1999,1998,1997,1996,1995,1994,1993,1992,1991,1990,1989,1988,1987,1986,1985,1984,1983,1982,1981,1980
Afghanistan,WB_LI,EMR,62,62,62,60,57,59,64,62,60,59,55,53,50,48,39,35,37,27,31,31,38,42,41,40,25,22,19,20,22,34,31,14,14,14,9,8,,11
Albania,WB_LMI,EUR,96,96,97,98,99,98,99,99,97,98,97,95,97,96,93,96,95,95,85,89,95,92,91,90,76,87,80,88,96,96,96,96,96,96,96,93,90,90
Algeria,WB_UMI,AFR,88,94,95,95,95,95,95,95,92,88,92,91,83,81,84,81,83,80,83,84,92,90,89,88,87,86,85,83,82,81,73,67,68,,,,,
Andorra,WB_HI,EUR,99,97,96,96,95,98,99,99,98,98,94,91,94,98,96,98,97,97,97,90,90,,,,,,,,,,,,,,,,,
Angola,WB_LMI,AFR,42,45,51,56,59,67,60,67,53,57,68,30,29,50,50,64,64,35,42,60,78,62,46,44,47,39,39,38,48,56,55,44,44,35,26,,,
Antigua and Barbuda,WB_HI,AMR,88,88,91,93,96,98,99,98,99,99,99,99,99,97,99,99,97,95,99,99,93,99,93,91,99,99,87,89,95,95,89,80,69,73,48,,,
Argentina,WB_UMI,AMR,89,90,89,95,94,94,95,95,96,96,94,97,98,99,97,95,89,91,97,95,93,99,99,96,95,92,99,93,89,87,80,87,54,79,67,67,77,61
Armenia,WB_LMI,EUR,96,97,97,97,97,97,97,97,96,94,92,92,94,92,94,91,93,92,92,94,92,89,96,95,95,93,,,,,,,,,,,,
Australia,WB_HI,WPR,95,95,95,94,94,94,94,94,94,94,94,94,94,94,94,94,92,91,88,82,85,87,87,86,86,86,86,86,85,77,68,68,68,68,68,,,
Austria,WB_HI,EUR,96,95,96,96,92,88,84,80,76,83,79,80,75,74,79,78,79,75,65,78,90,87,60,60,60,60,60,60,60,60,60,60,40,30,25,25,25,
Azerbaijan,WB_LMI,EUR,98,98,98,98,98,94,92,89,85,79,75,69,67,67,67,66,68,67,68,67,66,65,64,60,49,52,,,,,,,,,,,,

I have this as a data set and I want to know if I can print all the countries who's World_Bank_Income_Level is = WB_LI

I am using pandas and when I put the CSV into a dataframe, even when I try to loop and find countries with that specification, I end up plotting the entire CSV file. I haven't found any way to only get it to plot countries with that specification. I think I would need to make a dataframe and append countries with that condition only contains but I'm not sure if that is right or how to do it.

So, How can I graph only specific rows?

import pandas as pd
import matplotlib.pyplot as plt
import csv

measles = pd.read_csv('measles.csv')

df = pd.DataFrame(measles)

print(df)

ax = plt.gca()

df.plot(kind='bar',x='Country',y='2017', color='red', ax=ax)

plt.show()
  • 2
    `df[df['World_Bank_Income_Level' == 'WB_LI']]` this should give the rows where `World_Bank_Income_Level` is `WB_LI` – deadshot Aug 19 '20 at 05:23
  • post the code you have tried so someone can help you is there any mistake in the code. without code it would be impossible – deadshot Aug 19 '20 at 05:26
  • Thank you I am new here. As for my code, I thought it would be useless because it doesn't even make sense to me right now. I will keep this in mind for next time. – Charminster Aug 19 '20 at 05:38
  • Update: I Need help with graphing the countries where World_Bank_Income_Level is WB_LI. I understand how to access those, but do I need to build a new dataframe in order to only graph those. I want to graph the 'Country' and '2017' column but for only those countries. – Charminster Aug 19 '20 at 05:55
  • it depends on your use case. but assigning to new dataframe is simple `df2 = df[df['World_Bank_Income_Level'] == 'WB_LI']` then you can use `df2.plot(...)` – deadshot Aug 19 '20 at 06:02

1 Answers1

0

No need to build new dataframe for it, just give the value to a new variable, like wb_country = df[df['World_Bank_Income_Level'] == 'WB_LI'], if you want more than one condition, you can try df[(df['World_Bank_Income_Level'] == 'WB_LI') & (what you need)]

Binke
  • 29
  • 7