2015-03-03 70 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