2015-03-03 34 views
0

我有一堆統計外貿數據堆積在單個表/ csv中:
年,is_export(否則是進口),國家,海關編碼,宏碼(一組海關編碼)和價值(以美元計)。熊貓統計普通表

我很想能夠使用大熊貓組數據(而不是使用普通的SQL),並得到如下:

macro_group=12 

2012 2013 2014 
country 
export 

難道我只需要做幾個groupby電話(在「鍵「我想建立一個層次結構)?

編輯:所有的行是相同的:

id|Country|Year|Export|Macro|Code|Codename|Value 
1|China|2012|1|69|6996700|Articles,of iron or steel wire,n.e.s.|0.0 
2|Germany|2012|1|69|6996700|Articles,of iron or steel wire,n.e.s.|59.9 
3|Italy|2012|1|69|6996700|Articles,of iron or steel wire,n.e.s.|33.2 

我想獲得的是:

**Macro e.g. 23** 
China total export 
2012 2013 2014 
432 34 3243 

China total import 
2012 2013 2014 
4534 345 4354 

Russia total import... 

+3

一些最起碼的測試數據一起玩會有幫助;) – Matt 2015-03-03 08:57:57

+1

你想要的輸出並不能真正告訴我很多你能解釋它顯示的是什麼 – EdChum 2015-03-03 09:16:59

回答

1

這並不完全清楚你期望的輸出是什麼(給定你提供的數據)。我猜你想要每個國家和年份的總價值(如果沒有,請隨時指正):

import pandas as pd 

########### Setup some test data: ############# 
s = """id|Country|Year|Export|Macro|Code|Codename|Value 
1|China|2012|1|69|6996700|Articles,of iron or steel wire,n.e.s.|0.0 
2|Germany|2012|1|69|6996700|Articles,of iron or steel wire,n.e.s.|59.9 
3|Germany|2013|1|69|6996700|Articles,of iron or steel wire,n.e.s.|80.0 
4|Germany|2013|1|69|6996700|Articles,of iron or steel wire,n.e.s.|40.0 
5|Italy|2012|1|69|6996700|Articles,of iron or steel wire,n.e.s.|33.2""" 

from StringIO import StringIO 
df = pd.read_csv(StringIO(s), sep='|') 

pd.Series.__unicode__ = pd.Series.to_string # suppress meta-data when printing 

########### The real stuff happens here: ############# 
macro = 69 
group_by = df[df.Macro == macro].groupby(['Country', 'Year'])['Value'].sum() 

for country in df.Country.unique(): 
    print '---', country, '---' 
    print group_by[country] 
    print 

導致下面的輸出:

--- China --- 
2012 0 

--- Germany --- 
2012  59.9 
2013 120.0 

--- Italy --- 
2012 33.2