In [1]:
%load_ext watermark
%watermark -d -a 'Sebastian Raschka' -v
This notebook is all about exploratory data analyses based on the new and awesome Star Wars API by Paul Hallett.
In [8]:
import requests
import json
In [9]:
d = {'name':[], 'diameter':[], 'population':[]}
url = "http://swapi.co/api/planets/?page=1"
while url:
r = requests.get(url)
data = json.loads(r.text)
for i in data['results']:
d['name'].append(i['name'])
d['diameter'].append(i['diameter'],)
d['population'].append(i['population']),
url = data['next']
In [10]:
import pandas as pd
df = pd.DataFrame(d)
df = df[(df['diameter']!='unknown') & (df['population']!='unknown')]
df['diameter'] = df['diameter'].astype(int)
df['population'] = df['population'].astype(int)
df.head()
Out[10]:
In [11]:
%matplotlib inline
In [14]:
import plotly.plotly as py
from random import random, randrange, seed
seed(456)
traces = []
for i in df.index:
tr = Scatter(
x=df.ix[i]['diameter'],
y=df.ix[i]['population'],
name=df.ix[i]['name'],
marker=Marker(
color='rgb(%s, %s, %s)' % (randrange(0,256), randrange(0,256), randrange(0,256)),
size=0.001*df.ix[i]['diameter']))
traces.append(tr)
layout = Layout(
title='Star Wars API - Planets',
xaxis=XAxis(
showgrid=False,
zeroline=False,
title='Diameter [km]',
tick0=-1,
type='log'),
yaxis=YAxis(
showgrid=False,
zeroline=False,
title='Population',
type='log'))
fig = Figure(data=traces, layout=layout)
py.iplot(fig, filename='SWAPI-Planets')
Out[14]:
In [2]:
import requests
import json
In [3]:
d = {'name':[], 'mass':[], 'height':[]}
url = "http://swapi.co/api/people/?page=1"
while url:
r = requests.get(url)
data = json.loads(r.text)
for i in data['results']:
d['name'].append(i['name'])
d['mass'].append(i['mass'],)
d['height'].append(i['height']),
url = data['next']
In [4]:
import pandas as pd
df = pd.DataFrame(d)
df = df[(df['mass']!='unknown') & (df['height']!='unknown')]
df['mass'] = df['mass'].apply(lambda x: x.replace(',','.'))
df['mass'] = df['mass'].astype(float)
df['height'] = df['height'].astype(int)
df.head()
Out[4]:
In [6]:
import plotly.plotly as py
from plotly.graph_objs import *
import plotly.tools as tls
from random import random, randrange, seed
seed(123)
traces = []
for i in df.index:
tr = Scatter(
x=df.ix[i]['height'],
y=df.ix[i]['mass'],
name=df.ix[i]['name'],
marker=Marker(
color='rgb(%s, %s, %s)' % (randrange(0,256), randrange(0,256), randrange(0,256)),
size=df.ix[i]['mass']/df.ix[i]['height']*20))
traces.append(tr)
layout = Layout(
title='Star Wars API - People',
showlegend=False,
xaxis=XAxis(
showgrid=False,
zeroline=False,
tick0=-1,
title='Mass [kg]',),
yaxis=YAxis(
showgrid=False,
zeroline=False,
title='Height [cm]',))
fig = Figure(data=traces, layout=layout)
py.iplot(fig, filename='SWAPI-People')
Out[6]:
In [12]:
import requests
import json
In [13]:
d = {'name':[], 'max_atmosphering_speed':[], 'hyperdrive_rating':[]}
url = "http://swapi.co/api/starships/?page=1"
while url:
r = requests.get(url)
data = json.loads(r.text)
for i in data['results']:
d['name'].append(i['name'])
d['max_atmosphering_speed'].append(i['max_atmosphering_speed'],)
d['hyperdrive_rating'].append(i['hyperdrive_rating']),
url = data['next']
In [14]:
import pandas as pd
df = pd.DataFrame(d)
df['max_atmosphering_speed'] = df['max_atmosphering_speed'].apply(lambda x: x.strip('km'))
df = df[(df['max_atmosphering_speed']!='n/a') & (df['hyperdrive_rating']!='n/a')]
df = df[(df['max_atmosphering_speed']!='unknown') & (df['hyperdrive_rating']!='unknown')]
df['max_atmosphering_speed'] = df['max_atmosphering_speed'].astype(float)
df['hyperdrive_rating'] = df['hyperdrive_rating'].astype(float)
df.head()
Out[14]:
In [15]:
import plotly.plotly as py
from plotly.graph_objs import *
import plotly.tools as tls
from random import random, randrange, seed
seed(123)
traces = []
for i in df.index:
tr = Scatter(
x=df.ix[i]['max_atmosphering_speed'],
y=df.ix[i]['hyperdrive_rating'],
name=df.ix[i]['name'],
marker=Marker(
color='rgb(%s, %s, %s)' % (randrange(0,256), randrange(0,256), randrange(0,256)),
size=0.001 * df.ix[i]['max_atmosphering_speed']/df.ix[i]['hyperdrive_rating']*20))
traces.append(tr)
layout = Layout(
title='Star Wars API - Spaceships',
showlegend=False,
xaxis=XAxis(
showgrid=False,
zeroline=False,
title='Max. atmosphering speed [km/h]',
tick0=-1,
type='log'),
yaxis=YAxis(
showgrid=False,
zeroline=False,
title='Hyperdrive rating',),)
fig = Figure(data=traces, layout=layout)
py.iplot(fig, filename='SWAPI-Spaceships')
Out[15]:
In [ ]: