Skip to content

Instantly share code, notes, and snippets.

@netanel246
Created March 31, 2016 21:53
Show Gist options
  • Save netanel246/03aa0b7eb31c0be326bea58c8f8ded82 to your computer and use it in GitHub Desktop.
Save netanel246/03aa0b7eb31c0be326bea58c8f8ded82 to your computer and use it in GitHub Desktop.
import json
import urllib2 as url
import pandas as pnd
import numpy as np
# Get the data
json_url="http://knoema.com/api/1.0/data/FAOFSDFI2012?Time=2000-2007&food-item=1000030,1000520,1000120,1000080,1000110,1000540,1000550&country=1000550,1000560,1000770,1001680,1001660,1000010,1000020,1000030,1000040,1000050,1000060,1000070,1000080,1000090,1000100,1000110,1000120,1000130,1000140,1000150,1000160,1000170,1000180,1000190,1000200,1000210,1000220,1000230,1000240,1000250,1000260,1000270,1000280,1000290,1000300" \
",1000310,1000320,1000330,1000340,1000350,1000360,1000370,1000380,1000390,1000400,1000410,1000420,1000430,1000440,1000450,1000460,1000470,1000480,1000490,1000500,1000510,1000520,1000530,1000540,1000570,1000580,1000590,1000600,1000610,1000620,1000630,1000640,1000650,1000660,1000670,1000680,1000690,1000700,1000710,1000720,1000730,1000740,1000750,1000760,1000780,1000790,1000800,1000810,1000820,1000830,1000840,1000850" \
",1000860,1000870,1000880,1000890,1000900,1000910,1000920,1000930,1000940,1000950,1000960,1000970,1000980,1000990,1001000,1001010,1001020,1001030,1001040,1001050,1001060,1001070,1001080,1001090,1001100,1001110,1001120,1001130,1001140,1001150,1001160,1001170,1001180,1001190,1001200,1001210,1001220,1001230,1001240,1001250,1001260,1001270,1001280,1001290,1001300,1001310,1001320,1001330,1001340,1001350,1001360,1001370,1001380" \
",1001390,1001400,1001410,1001420,1001430,1001440,1001450,1001460,1001470,1001480,1001490,1001500,1001510,1001520,1001530,1001540,1001550,1001560,1001570,1001580,1001590,1001600,1001610,1001620,1001630,1001640,1001650,1001670,1001690,1001700,1001710,1001720,1001730,1001740,1001750,1001760&variable=1000020&Frequencies=A"
u = url.urlopen(json_url)
data = json.load(u)
df = pnd.DataFrame(data["data"])
agg_group = df.groupby(["country", "food-item"])
# Average food consumption segmentation by country
value_agg = agg_group["Value"].aggregate([np.mean])
print value_agg.loc[["France", "Israel"]]
@look4regev
Copy link

Thanks! Great and simple example for aggregations with pandas!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment