2016-09-14 19 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上的人均收入。並且可能選擇某個特定年份的圖表將會改變。

相關問題