Skip to content

Instantly share code, notes, and snippets.

@kretes
Created November 14, 2016 09:39
Show Gist options
  • Save kretes/2c191dddd78f8b5dcf20f3841eda24db to your computer and use it in GitHub Desktop.
Save kretes/2c191dddd78f8b5dcf20f3841eda24db to your computer and use it in GitHub Desktop.
Script to get Global Peace Index (GPI) data from WIkipedia as pandas DF
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 57,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import requests\n",
"from bs4 import BeautifulSoup\n",
"\n",
"response = requests.get(url='https://en.wikipedia.org/wiki/Global_Peace_Index')\n",
"\n",
"soup = BeautifulSoup(response.text, 'html.parser')\n",
"\n",
"base_year = 2016\n",
"years = 9\n",
"\n",
"def get_countries_by_gpi():\n",
" for table in soup.find_all('table'):\n",
" if table.find_all('th')[0].get_text() == 'Country':\n",
" for tr in table.find_all('tr'):\n",
" country_name = tr.find_all('a')[0].get_text()\n",
" if not country_name.startswith('['):\n",
" row = {'country': country_name}\n",
" for year, index in zip(range(base_year-years+1,base_year+1),range(2*(years),0,-2)):\n",
" score = tr.find_all('td')[index].get_text()\n",
" if score != '':\n",
" row['score_%s' % year] = float(score)\n",
" yield row\n",
"\n",
"import pandas as pd\n",
"gpi = pd.DataFrame.from_dict(list(get_countries_by_gpi()))\n",
"gpi.to_csv('gpi_%s-%s.csv' % (base_year-years+1,base_year),index=False)"
]
},
{
"cell_type": "code",
"execution_count": 58,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>country</th>\n",
" <th>score_2008</th>\n",
" <th>score_2009</th>\n",
" <th>score_2010</th>\n",
" <th>score_2011</th>\n",
" <th>score_2012</th>\n",
" <th>score_2013</th>\n",
" <th>score_2014</th>\n",
" <th>score_2015</th>\n",
" <th>score_2016</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>Iceland</td>\n",
" <td>1.107</td>\n",
" <td>1.203</td>\n",
" <td>1.212</td>\n",
" <td>1.148</td>\n",
" <td>1.113</td>\n",
" <td>1.162</td>\n",
" <td>1.189</td>\n",
" <td>1.148</td>\n",
" <td>1.192</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>Denmark</td>\n",
" <td>1.238</td>\n",
" <td>1.263</td>\n",
" <td>1.341</td>\n",
" <td>1.289</td>\n",
" <td>1.239</td>\n",
" <td>1.207</td>\n",
" <td>1.193</td>\n",
" <td>1.150</td>\n",
" <td>1.246</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>Austria</td>\n",
" <td>1.291</td>\n",
" <td>1.240</td>\n",
" <td>1.290</td>\n",
" <td>1.337</td>\n",
" <td>1.328</td>\n",
" <td>1.250</td>\n",
" <td>1.200</td>\n",
" <td>1.198</td>\n",
" <td>1.278</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>New Zealand</td>\n",
" <td>1.190</td>\n",
" <td>1.227</td>\n",
" <td>1.188</td>\n",
" <td>1.279</td>\n",
" <td>1.239</td>\n",
" <td>1.237</td>\n",
" <td>1.236</td>\n",
" <td>1.221</td>\n",
" <td>1.287</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>Portugal</td>\n",
" <td>1.385</td>\n",
" <td>1.426</td>\n",
" <td>1.366</td>\n",
" <td>1.453</td>\n",
" <td>1.470</td>\n",
" <td>1.467</td>\n",
" <td>1.425</td>\n",
" <td>1.344</td>\n",
" <td>1.356</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" country score_2008 score_2009 score_2010 score_2011 score_2012 \\\n",
"0 Iceland 1.107 1.203 1.212 1.148 1.113 \n",
"1 Denmark 1.238 1.263 1.341 1.289 1.239 \n",
"2 Austria 1.291 1.240 1.290 1.337 1.328 \n",
"3 New Zealand 1.190 1.227 1.188 1.279 1.239 \n",
"4 Portugal 1.385 1.426 1.366 1.453 1.470 \n",
"\n",
" score_2013 score_2014 score_2015 score_2016 \n",
"0 1.162 1.189 1.148 1.192 \n",
"1 1.207 1.193 1.150 1.246 \n",
"2 1.250 1.200 1.198 1.278 \n",
"3 1.237 1.236 1.221 1.287 \n",
"4 1.467 1.425 1.344 1.356 "
]
},
"execution_count": 58,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"gpi[:5]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.1"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment