Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

Jupyter notebook Learn to code OU/Week4/2016-11-14-115800.ipynb

Project: 161004 test
Views: 60
Kernel: Anaconda (Python 3)
import warnings warnings.simplefilter('ignore', FutureWarning) from pandas import *
data=[['foo','one','small',1], ['foo','one','large',2], ['foo','one','large',2], ['foo','two','small',3], ['foo','two','small',3], ['bar','one','large',4], ['bar','one','small',5], ['bar','two','small',6], ['foo','one','large',2], ['foo','one','large',2], ['foo','two','small',3], ['foo','two','small',3], ['bar','one','large',4], ['bar','one','small',5], ['foo','one','large',2], ['foo','one','large',2], ['foo','two','small',3], ['foo','two','small',3], ['bar','one','large',4], ['bar','one','small',5], ['bar','two','small',6], ['bar','two','small',6], ['bar','two','large',7]] df=DataFrame(data=data, columns=['A','B','C','D']) df.head()
A B C D
0 foo one small 1
1 foo one large 2
2 foo one large 2
3 foo two small 3
4 foo two small 3
LOCATION='comtrade_milk_uk_monthly_14.csv'
milk = read_csv(LOCATION, dtype={'Commodity Code':str, 'Reporter Code':str}) milk.head(3)
Classification Year Period Period Desc. Aggregate Level Is Leaf Code Trade Flow Code Trade Flow Reporter Code Reporter ... Qty Alt Qty Unit Code Alt Qty Unit Alt Qty Netweight (kg) Gross weight (kg) Trade Value (US$) CIF Trade Value (US$) FOB Trade Value (US$) Flag
0 HS 2014 201401 January 2014 4 0 1 Imports 826 United Kingdom ... NaN NaN NaN NaN 22404316 NaN 21950747 NaN NaN 0
1 HS 2014 201401 January 2014 4 0 2 Exports 826 United Kingdom ... NaN NaN NaN NaN 60497363 NaN 46923551 NaN NaN 0
2 HS 2014 201401 January 2014 4 0 2 Exports 826 United Kingdom ... NaN NaN NaN NaN 2520 NaN 3410 NaN NaN 0

3 rows × 35 columns

COLUMNS = ['Year', 'Period','Trade Flow','Reporter', 'Partner', 'Commodity','Commodity Code','Trade Value (US$)'] milk = milk[COLUMNS] milk.head()
Year Period Trade Flow Reporter Partner Commodity Commodity Code Trade Value (US$)
0 2014 201401 Imports United Kingdom World Milk and cream, neither concentrated nor sweet... 0401 21950747
1 2014 201401 Exports United Kingdom World Milk and cream, neither concentrated nor sweet... 0401 46923551
2 2014 201401 Exports United Kingdom Afghanistan Milk and cream, neither concentrated nor sweet... 0401 3410
3 2014 201401 Exports United Kingdom Austria Milk and cream, neither concentrated nor sweet... 0401 316
4 2014 201401 Imports United Kingdom Belgium Milk and cream, neither concentrated nor sweet... 0401 4472349
milk_world = milk[milk['Partner'] == 'World'] milk_countries = milk[milk['Partner'] != 'World'] #milk_world.head() #milk_countries.head()
#Chris's sandbox #def weedingOut(g):# TypeError: filter function returned a Series, but expected a scalar bool # return ((g['Partner']=='Netherlands') & (g['Trade Flow']=='Exports')) #& (g['Period']<201405)) # filter out Partner == 'Netherlands # filter out == 'Exports # filter out 'Period' < 201405 milk_countries.head()# head works OK result = milk_countries.groupby(['Trade Flow','Partner'])#.filter(weedingOut) #type(result)# pandas.core.groupby.DataFrameGroupBy result.head()# heade returns full table #get_option("display.max_rows")
Year Period Trade Flow Reporter Partner Commodity Commodity Code Trade Value (US$)
2 2014 201401 Exports United Kingdom Afghanistan Milk and cream, neither concentrated nor sweet... 0401 3410
3 2014 201401 Exports United Kingdom Austria Milk and cream, neither concentrated nor sweet... 0401 316
4 2014 201401 Imports United Kingdom Belgium Milk and cream, neither concentrated nor sweet... 0401 4472349
5 2014 201401 Exports United Kingdom Belgium Milk and cream, neither concentrated nor sweet... 0401 5663128
6 2014 201401 Exports United Kingdom Br. Virgin Isds Milk and cream, neither concentrated nor sweet... 0401 34566
7 2014 201401 Exports United Kingdom Canada Milk and cream, neither concentrated nor sweet... 0401 51017
8 2014 201401 Exports United Kingdom China Milk and cream, neither concentrated nor sweet... 0401 265039
9 2014 201401 Exports United Kingdom Cyprus Milk and cream, neither concentrated nor sweet... 0401 18738
10 2014 201401 Imports United Kingdom Denmark Milk and cream, neither concentrated nor sweet... 0401 2233438
11 2014 201401 Exports United Kingdom Denmark Milk and cream, neither concentrated nor sweet... 0401 417
12 2014 201401 Exports United Kingdom Equatorial Guinea Milk and cream, neither concentrated nor sweet... 0401 3201
13 2014 201401 Exports United Kingdom Falkland Isds (Malvinas) Milk and cream, neither concentrated nor sweet... 0401 26614
14 2014 201401 Exports United Kingdom Finland Milk and cream, neither concentrated nor sweet... 0401 141697
15 2014 201401 Imports United Kingdom France Milk and cream, neither concentrated nor sweet... 0401 1522872
16 2014 201401 Exports United Kingdom France Milk and cream, neither concentrated nor sweet... 0401 2919079
17 2014 201401 Imports United Kingdom Germany Milk and cream, neither concentrated nor sweet... 0401 1028700
18 2014 201401 Exports United Kingdom Germany Milk and cream, neither concentrated nor sweet... 0401 982240
19 2014 201401 Exports United Kingdom Gibraltar Milk and cream, neither concentrated nor sweet... 0401 11079
20 2014 201401 Exports United Kingdom Greece Milk and cream, neither concentrated nor sweet... 0401 20
21 2014 201401 Exports United Kingdom China, Hong Kong SAR Milk and cream, neither concentrated nor sweet... 0401 295201
22 2014 201401 Exports United Kingdom Hungary Milk and cream, neither concentrated nor sweet... 0401 4735
23 2014 201401 Imports United Kingdom Ireland Milk and cream, neither concentrated nor sweet... 0401 10676138
24 2014 201401 Exports United Kingdom Ireland Milk and cream, neither concentrated nor sweet... 0401 32069689
25 2014 201401 Imports United Kingdom Italy Milk and cream, neither concentrated nor sweet... 0401 4685
26 2014 201401 Exports United Kingdom Italy Milk and cream, neither concentrated nor sweet... 0401 947
27 2014 201401 Exports United Kingdom Rep. of Korea Milk and cream, neither concentrated nor sweet... 0401 2825
28 2014 201401 Imports United Kingdom Lithuania Milk and cream, neither concentrated nor sweet... 0401 5629
29 2014 201401 Exports United Kingdom Luxembourg Milk and cream, neither concentrated nor sweet... 0401 26
30 2014 201401 Exports United Kingdom Malta Milk and cream, neither concentrated nor sweet... 0401 9748
31 2014 201401 Exports United Kingdom Other Asia, nes Milk and cream, neither concentrated nor sweet... 0401 4372
... ... ... ... ... ... ... ... ...
1307 2014 201410 Exports United Kingdom Philippines Milk and cream, concentrated or sweetened 0402 79761
1335 2014 201411 Exports United Kingdom Australia Milk and cream, concentrated or sweetened 0402 2639
1336 2014 201411 Imports United Kingdom Austria Milk and cream, concentrated or sweetened 0402 144989
1338 2014 201411 Exports United Kingdom Bangladesh Milk and cream, concentrated or sweetened 0402 701736
1341 2014 201411 Exports United Kingdom Cameroon Milk and cream, concentrated or sweetened 0402 177395
1344 2014 201411 Exports United Kingdom Cuba Milk and cream, concentrated or sweetened 0402 933206
1350 2014 201411 Exports United Kingdom Dominica Milk and cream, concentrated or sweetened 0402 48359
1351 2014 201411 Exports United Kingdom Dominican Rep. Milk and cream, concentrated or sweetened 0402 233666
1356 2014 201411 Exports United Kingdom Gabon Milk and cream, concentrated or sweetened 0402 50748
1357 2014 201411 Exports United Kingdom Gambia Milk and cream, concentrated or sweetened 0402 11985
1360 2014 201411 Exports United Kingdom Ghana Milk and cream, concentrated or sweetened 0402 90701
1362 2014 201411 Exports United Kingdom Guatemala Milk and cream, concentrated or sweetened 0402 63056
1372 2014 201411 Exports United Kingdom Jordan Milk and cream, concentrated or sweetened 0402 4354
1373 2014 201411 Exports United Kingdom Kenya Milk and cream, concentrated or sweetened 0402 66726
1376 2014 201411 Exports United Kingdom Latvia Milk and cream, concentrated or sweetened 0402 35
1380 2014 201411 Exports United Kingdom Mexico Milk and cream, concentrated or sweetened 0402 746926
1389 2014 201411 Exports United Kingdom Saint Kitts and Nevis Milk and cream, concentrated or sweetened 0402 3392
1390 2014 201411 Exports United Kingdom Saint Lucia Milk and cream, concentrated or sweetened 0402 64440
1394 2014 201411 Exports United Kingdom Slovakia Milk and cream, concentrated or sweetened 0402 68
1411 2014 201412 Exports United Kingdom Bangladesh Milk and cream, concentrated or sweetened 0402 95797
1414 2014 201412 Exports United Kingdom Myanmar Milk and cream, concentrated or sweetened 0402 6236
1415 2014 201412 Exports United Kingdom Cameroon Milk and cream, concentrated or sweetened 0402 170651
1420 2014 201412 Exports United Kingdom Cuba Milk and cream, concentrated or sweetened 0402 1916244
1424 2014 201412 Exports United Kingdom Dominican Rep. Milk and cream, concentrated or sweetened 0402 363535
1427 2014 201412 Exports United Kingdom Gabon Milk and cream, concentrated or sweetened 0402 68886
1431 2014 201412 Exports United Kingdom Guatemala Milk and cream, concentrated or sweetened 0402 64268
1449 2014 201412 Exports United Kingdom Mauritania Milk and cream, concentrated or sweetened 0402 116367
1451 2014 201412 Exports United Kingdom Namibia Milk and cream, concentrated or sweetened 0402 256335
1454 2014 201412 Exports United Kingdom Niger Milk and cream, concentrated or sweetened 0402 84543
1456 2014 201412 Exports United Kingdom Pakistan Milk and cream, concentrated or sweetened 0402 57483

601 rows × 8 columns

result.head??