2016-09-14 20 views
0

教授给出的指令: 1.使用来自World Atlas数据的continent国家列表,将countries.csv文件加载到pandas DataFrame中,并将该数据集命名为国家。 2.使用Gapminder上提供的数据,将每人的收入(GDP /人均,PPP $通货膨胀调整后)作为熊猫数据框载入,并将此数据集命名为收入。 3.将数据集转换为以行和国家作为列的年份。加载时显示该数据集的头部。 4.以图形方式显示任何特定年份(例如2000年)世界各国人均收入分布情况。什么样的情节最好?从Pandas中的一行中获取数据

在下面的代码中,我完成了其中一些任务,但我很难理解如何从DataFrame行获取数据。我希望能够从一行中获取数据,然后绘制它。这可能看起来像一个微不足道的概念,但我已经有一段时间了,需要帮助。

%matplotlib inline 
import numpy as np 
import pandas as pd 
import matplotlib.pyplot as plt 
countries = pd.read_csv('2014_data/countries.csv') 
countries.head(n=3) 
income = pd.read_excel('indicator gapminder gdp_per_capita_ppp.xlsx') 
income = income.T 

def graph_per_year(year): 
    stryear = str(year) 
    dfList = income[stryear].tolist() 
graph_per_year(1801) 
+0

http://pandas.pydata.org/pandas-docs/stable/indexing.html –

+0

您可以向我们展示'countries.head()'和'income.head()'的输出以供说明吗? – tipanverella

回答

1

熊猫使用三种类型的索引。

如果您正在寻找使用整数索引,你将需要使用.iloc

df_1 
Out[5]: 
      consId fan-cnt 
0 1155696024483  34.0 
1 1155699007557  34.0 
2 1155694005571  34.0 
3 1155691016680  12.0 
4 1155697016945  34.0 

df_1.iloc[1,:] #go to the row with index 1 and select all the columns 
Out[8]: 
consId  1.155699e+12 
fan-cnt 3.400000e+01 
Name: 1, dtype: float64 

而且去一个特定的细胞,可以使用大意如下的东西,

df_1.iloc[1][1] 
Out[9]: 34.0 

您需要通过documentation进行其他类型的索引,如sohier-dane建议的.ix.loc

0

要回答你的第一个问题,带有年份的条形图将是最好的。你必须保持y轴上的国家和y上的人均收入。并且可能选择某个特定年份的图表将会改变。

相关问题