简体   繁体   English

使用 pandas 从 CSV 绘制特定数据

[英]Plotting specific data from CSV using pandas

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我有这个作为数据集,我想知道我是否可以打印所有 World_Bank_Income_Level 为 = 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 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.我还没有找到任何方法只能将它带到具有该规范的 plot 国家/地区。 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.我想我需要制作一个 dataframe 和 append 国家,但我不确定这是否正确或如何去做。

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()

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)]无需为它构建新的 dataframe,只需将值赋给一个新变量,如 wb_country = df[df['World_Bank_Income_Level'] == 'WB_LI'],如果您想要多个条件,可以尝试 df[( df['World_Bank_Income_Level'] == 'WB_LI') & (你需要什么)]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM