import requests
import json
import pandas
import matplotlib.pyplot as plt
import numpy as np
from pandas.io.json import json_normalize
pandas.set_option('display.max_rows', 500)
colors='YlGn_r'
colors2='YlGn'
colors3='dimgrey'
url = 'https://the-one-api.herokuapp.com/v1/character'
headers = {"Authorization": "Bearer 16TtGwhAHbABjZtS6bgu"}
response = requests.get(url, headers=headers)
results = response.json()
LOTR_characters = json_normalize(results)
LOTR_characters = json_normalize(LOTR_characters.docs[0])
LOTR_characters.head()
LOTR_characters.shape
LOTR_characters[LOTR_characters.name=='Bilbo Baggins']
LOTR_characters['gender']=LOTR_characters['gender'].replace('Males','Male')
LOTR_characters['gender']=LOTR_characters['gender'].replace('Most likely male','Male')
LOTR_characters['gender']=LOTR_characters['gender'].replace('male','Male')
LOTR_characters['gender']=LOTR_characters['gender'].replace(np.nan,'Unknown')
LOTR_characters['gender']=LOTR_characters['gender'].replace('','Unknown')
all_women = LOTR_characters[LOTR_characters.gender=='Female']['_id'].count()
all_women
round(all_women/LOTR_characters.shape[0],2)*100
all_men = LOTR_characters[LOTR_characters.gender=='Male']['_id'].count()
all_men
round(all_men/LOTR_characters.shape[0],2)*100
LOTR_characters['gender'].value_counts().plot.pie(autopct='%1.0f%%',pctdistance=0.5, labeldistance=1.2, cmap=colors, textprops={'color':colors3})
set(LOTR_characters['death'].sample(n=100))
LOTR_characters['death']=LOTR_characters['death'].replace('None','')
LOTR_characters['death']=LOTR_characters['death'].str.replace(r'(^.*alive.*$)','', case=False)
LOTR_characters['death']=LOTR_characters['death'].replace('',np.nan)
LOTR_characters.death.isnull().value_counts()
round(LOTR_characters[LOTR_characters.death.isnull()].count()['_id']/LOTR_characters.shape[0],2)*100
alive_women = LOTR_characters[LOTR_characters.gender=='Female']['death'].isna().sum()
alive_women
round(alive_women/all_women,2)*100
alive_men = LOTR_characters[LOTR_characters.gender=='Male']['death'].isna().sum()
alive_men
round(alive_men/all_men,2)*100
LOTR_characters[LOTR_characters['death'].notnull()]['gender'].value_counts().plot.pie(autopct='%1.0f%%',pctdistance=0.5, labeldistance=1.2, cmap=colors, textprops={'color':colors3})
LOTR_characters['race']=LOTR_characters['race'].replace('Hobbits','Hobbit')
LOTR_characters['race']=LOTR_characters['race'].replace('Ents','Ent')
LOTR_characters['race']=LOTR_characters['race'].replace('Men','Human')
LOTR_characters['race']=LOTR_characters['race'].replace('Orcs','Orc')
LOTR_characters['race']=LOTR_characters['race'].replace('Eagles','Eagle')
LOTR_characters['race']=LOTR_characters['race'].replace('Uruk-hai,Orc','Uruk-hai')
LOTR_characters['race']=LOTR_characters['race'].replace('Great Eagle','Eagle')
LOTR_characters['race']=LOTR_characters['race'].replace('Elves','Elf')
LOTR_characters['race']=LOTR_characters['race'].replace('Dwarves','Dwarf')
LOTR_characters['race']=LOTR_characters['race'].replace('Men,Wraith','Human')
LOTR_characters['race']=LOTR_characters['race'].replace('','Unknown')
LOTR_characters['race']=LOTR_characters['race'].replace(np.nan,'Unknown')
set(LOTR_characters['race'])
LOTR_characters['race'].value_counts().count()
principal_races=['Human','Elf','Hobbit','Dwarf','Orc']
LOTR_characters.loc[~LOTR_characters['race'].isin(principal_races), 'race'] = 'Other'
LOTR_characters['race'].value_counts().plot.pie(autopct='%1.0f%%',pctdistance=0.5, cmap=colors2, textprops={'color':colors3})
LOTR_characters[LOTR_characters['death'].notnull()]['race'].value_counts().plot.pie(autopct='%1.0f%%',pctdistance=0.5, cmap=colors2,textprops={'color':colors3})
gender = LOTR_characters.groupby(['race','gender'])['_id'].count().unstack('gender')
gender.plot(kind='bar', stacked=True, cmap=colors2)