This example will read from the star wars api http://swapi.co/api/people/ into a Pandas DataFrame
In [1]:
    
import pandas as pd
import requests
    
In [2]:
    
# function to get star wars characters
def GetStarWarsCharacters():
    response = requests.get('http://swapi.co/api/people/')
    data_dict = response.json()
    characters = data_dict['results']
    return characters
    
In [3]:
    
# let's try out our function. notice it's a list of dictionary.
GetStarWarsCharacters()
    
    Out[3]:
[{'birth_year': '19BBY',
  'created': '2014-12-09T13:50:51.644000Z',
  'edited': '2014-12-20T21:17:56.891000Z',
  'eye_color': 'blue',
  'films': ['http://swapi.co/api/films/6/',
   'http://swapi.co/api/films/3/',
   'http://swapi.co/api/films/2/',
   'http://swapi.co/api/films/1/',
   'http://swapi.co/api/films/7/'],
  'gender': 'male',
  'hair_color': 'blond',
  'height': '172',
  'homeworld': 'http://swapi.co/api/planets/1/',
  'mass': '77',
  'name': 'Luke Skywalker',
  'skin_color': 'fair',
  'species': ['http://swapi.co/api/species/1/'],
  'starships': ['http://swapi.co/api/starships/12/',
   'http://swapi.co/api/starships/22/'],
  'url': 'http://swapi.co/api/people/1/',
  'vehicles': ['http://swapi.co/api/vehicles/14/',
   'http://swapi.co/api/vehicles/30/']},
 {'birth_year': '112BBY',
  'created': '2014-12-10T15:10:51.357000Z',
  'edited': '2014-12-20T21:17:50.309000Z',
  'eye_color': 'yellow',
  'films': ['http://swapi.co/api/films/5/',
   'http://swapi.co/api/films/4/',
   'http://swapi.co/api/films/6/',
   'http://swapi.co/api/films/3/',
   'http://swapi.co/api/films/2/',
   'http://swapi.co/api/films/1/'],
  'gender': 'n/a',
  'hair_color': 'n/a',
  'height': '167',
  'homeworld': 'http://swapi.co/api/planets/1/',
  'mass': '75',
  'name': 'C-3PO',
  'skin_color': 'gold',
  'species': ['http://swapi.co/api/species/2/'],
  'starships': [],
  'url': 'http://swapi.co/api/people/2/',
  'vehicles': []},
 {'birth_year': '33BBY',
  'created': '2014-12-10T15:11:50.376000Z',
  'edited': '2014-12-20T21:17:50.311000Z',
  'eye_color': 'red',
  'films': ['http://swapi.co/api/films/5/',
   'http://swapi.co/api/films/4/',
   'http://swapi.co/api/films/6/',
   'http://swapi.co/api/films/3/',
   'http://swapi.co/api/films/2/',
   'http://swapi.co/api/films/1/',
   'http://swapi.co/api/films/7/'],
  'gender': 'n/a',
  'hair_color': 'n/a',
  'height': '96',
  'homeworld': 'http://swapi.co/api/planets/8/',
  'mass': '32',
  'name': 'R2-D2',
  'skin_color': 'white, blue',
  'species': ['http://swapi.co/api/species/2/'],
  'starships': [],
  'url': 'http://swapi.co/api/people/3/',
  'vehicles': []},
 {'birth_year': '41.9BBY',
  'created': '2014-12-10T15:18:20.704000Z',
  'edited': '2014-12-20T21:17:50.313000Z',
  'eye_color': 'yellow',
  'films': ['http://swapi.co/api/films/6/',
   'http://swapi.co/api/films/3/',
   'http://swapi.co/api/films/2/',
   'http://swapi.co/api/films/1/'],
  'gender': 'male',
  'hair_color': 'none',
  'height': '202',
  'homeworld': 'http://swapi.co/api/planets/1/',
  'mass': '136',
  'name': 'Darth Vader',
  'skin_color': 'white',
  'species': ['http://swapi.co/api/species/1/'],
  'starships': ['http://swapi.co/api/starships/13/'],
  'url': 'http://swapi.co/api/people/4/',
  'vehicles': []},
 {'birth_year': '19BBY',
  'created': '2014-12-10T15:20:09.791000Z',
  'edited': '2014-12-20T21:17:50.315000Z',
  'eye_color': 'brown',
  'films': ['http://swapi.co/api/films/6/',
   'http://swapi.co/api/films/3/',
   'http://swapi.co/api/films/2/',
   'http://swapi.co/api/films/1/',
   'http://swapi.co/api/films/7/'],
  'gender': 'female',
  'hair_color': 'brown',
  'height': '150',
  'homeworld': 'http://swapi.co/api/planets/2/',
  'mass': '49',
  'name': 'Leia Organa',
  'skin_color': 'light',
  'species': ['http://swapi.co/api/species/1/'],
  'starships': [],
  'url': 'http://swapi.co/api/people/5/',
  'vehicles': ['http://swapi.co/api/vehicles/30/']},
 {'birth_year': '52BBY',
  'created': '2014-12-10T15:52:14.024000Z',
  'edited': '2014-12-20T21:17:50.317000Z',
  'eye_color': 'blue',
  'films': ['http://swapi.co/api/films/5/',
   'http://swapi.co/api/films/6/',
   'http://swapi.co/api/films/1/'],
  'gender': 'male',
  'hair_color': 'brown, grey',
  'height': '178',
  'homeworld': 'http://swapi.co/api/planets/1/',
  'mass': '120',
  'name': 'Owen Lars',
  'skin_color': 'light',
  'species': ['http://swapi.co/api/species/1/'],
  'starships': [],
  'url': 'http://swapi.co/api/people/6/',
  'vehicles': []},
 {'birth_year': '47BBY',
  'created': '2014-12-10T15:53:41.121000Z',
  'edited': '2014-12-20T21:17:50.319000Z',
  'eye_color': 'blue',
  'films': ['http://swapi.co/api/films/5/',
   'http://swapi.co/api/films/6/',
   'http://swapi.co/api/films/1/'],
  'gender': 'female',
  'hair_color': 'brown',
  'height': '165',
  'homeworld': 'http://swapi.co/api/planets/1/',
  'mass': '75',
  'name': 'Beru Whitesun lars',
  'skin_color': 'light',
  'species': ['http://swapi.co/api/species/1/'],
  'starships': [],
  'url': 'http://swapi.co/api/people/7/',
  'vehicles': []},
 {'birth_year': 'unknown',
  'created': '2014-12-10T15:57:50.959000Z',
  'edited': '2014-12-20T21:17:50.321000Z',
  'eye_color': 'red',
  'films': ['http://swapi.co/api/films/1/'],
  'gender': 'n/a',
  'hair_color': 'n/a',
  'height': '97',
  'homeworld': 'http://swapi.co/api/planets/1/',
  'mass': '32',
  'name': 'R5-D4',
  'skin_color': 'white, red',
  'species': ['http://swapi.co/api/species/2/'],
  'starships': [],
  'url': 'http://swapi.co/api/people/8/',
  'vehicles': []},
 {'birth_year': '24BBY',
  'created': '2014-12-10T15:59:50.509000Z',
  'edited': '2014-12-20T21:17:50.323000Z',
  'eye_color': 'brown',
  'films': ['http://swapi.co/api/films/1/'],
  'gender': 'male',
  'hair_color': 'black',
  'height': '183',
  'homeworld': 'http://swapi.co/api/planets/1/',
  'mass': '84',
  'name': 'Biggs Darklighter',
  'skin_color': 'light',
  'species': ['http://swapi.co/api/species/1/'],
  'starships': ['http://swapi.co/api/starships/12/'],
  'url': 'http://swapi.co/api/people/9/',
  'vehicles': []},
 {'birth_year': '57BBY',
  'created': '2014-12-10T16:16:29.192000Z',
  'edited': '2014-12-20T21:17:50.325000Z',
  'eye_color': 'blue-gray',
  'films': ['http://swapi.co/api/films/5/',
   'http://swapi.co/api/films/4/',
   'http://swapi.co/api/films/6/',
   'http://swapi.co/api/films/3/',
   'http://swapi.co/api/films/2/',
   'http://swapi.co/api/films/1/'],
  'gender': 'male',
  'hair_color': 'auburn, white',
  'height': '182',
  'homeworld': 'http://swapi.co/api/planets/20/',
  'mass': '77',
  'name': 'Obi-Wan Kenobi',
  'skin_color': 'fair',
  'species': ['http://swapi.co/api/species/1/'],
  'starships': ['http://swapi.co/api/starships/48/',
   'http://swapi.co/api/starships/59/',
   'http://swapi.co/api/starships/64/',
   'http://swapi.co/api/starships/65/',
   'http://swapi.co/api/starships/74/'],
  'url': 'http://swapi.co/api/people/10/',
  'vehicles': ['http://swapi.co/api/vehicles/38/']}]
In [4]:
    
# example of reading the star-wars api
characters = GetStarWarsCharacters() # This function gets the people as json
character_df = pd.DataFrame(characters)
character_df
    
    Out[4]:
  
    
       
      birth_year 
      created 
      edited 
      eye_color 
      films 
      gender 
      hair_color 
      height 
      homeworld 
      mass 
      name 
      skin_color 
      species 
      starships 
      url 
      vehicles 
     
  
  
    
      0 
      19BBY 
      2014-12-09T13:50:51.644000Z 
      2014-12-20T21:17:56.891000Z 
      blue 
      [http://swapi.co/api/films/6/, http://swapi.co... 
      male 
      blond 
      172 
      http://swapi.co/api/planets/1/ 
      77 
      Luke Skywalker 
      fair 
      [http://swapi.co/api/species/1/] 
      [http://swapi.co/api/starships/12/, http://swa... 
      http://swapi.co/api/people/1/ 
      [http://swapi.co/api/vehicles/14/, http://swap... 
     
    
      1 
      112BBY 
      2014-12-10T15:10:51.357000Z 
      2014-12-20T21:17:50.309000Z 
      yellow 
      [http://swapi.co/api/films/5/, http://swapi.co... 
      n/a 
      n/a 
      167 
      http://swapi.co/api/planets/1/ 
      75 
      C-3PO 
      gold 
      [http://swapi.co/api/species/2/] 
      [] 
      http://swapi.co/api/people/2/ 
      [] 
     
    
      2 
      33BBY 
      2014-12-10T15:11:50.376000Z 
      2014-12-20T21:17:50.311000Z 
      red 
      [http://swapi.co/api/films/5/, http://swapi.co... 
      n/a 
      n/a 
      96 
      http://swapi.co/api/planets/8/ 
      32 
      R2-D2 
      white, blue 
      [http://swapi.co/api/species/2/] 
      [] 
      http://swapi.co/api/people/3/ 
      [] 
     
    
      3 
      41.9BBY 
      2014-12-10T15:18:20.704000Z 
      2014-12-20T21:17:50.313000Z 
      yellow 
      [http://swapi.co/api/films/6/, http://swapi.co... 
      male 
      none 
      202 
      http://swapi.co/api/planets/1/ 
      136 
      Darth Vader 
      white 
      [http://swapi.co/api/species/1/] 
      [http://swapi.co/api/starships/13/] 
      http://swapi.co/api/people/4/ 
      [] 
     
    
      4 
      19BBY 
      2014-12-10T15:20:09.791000Z 
      2014-12-20T21:17:50.315000Z 
      brown 
      [http://swapi.co/api/films/6/, http://swapi.co... 
      female 
      brown 
      150 
      http://swapi.co/api/planets/2/ 
      49 
      Leia Organa 
      light 
      [http://swapi.co/api/species/1/] 
      [] 
      http://swapi.co/api/people/5/ 
      [http://swapi.co/api/vehicles/30/] 
     
    
      5 
      52BBY 
      2014-12-10T15:52:14.024000Z 
      2014-12-20T21:17:50.317000Z 
      blue 
      [http://swapi.co/api/films/5/, http://swapi.co... 
      male 
      brown, grey 
      178 
      http://swapi.co/api/planets/1/ 
      120 
      Owen Lars 
      light 
      [http://swapi.co/api/species/1/] 
      [] 
      http://swapi.co/api/people/6/ 
      [] 
     
    
      6 
      47BBY 
      2014-12-10T15:53:41.121000Z 
      2014-12-20T21:17:50.319000Z 
      blue 
      [http://swapi.co/api/films/5/, http://swapi.co... 
      female 
      brown 
      165 
      http://swapi.co/api/planets/1/ 
      75 
      Beru Whitesun lars 
      light 
      [http://swapi.co/api/species/1/] 
      [] 
      http://swapi.co/api/people/7/ 
      [] 
     
    
      7 
      unknown 
      2014-12-10T15:57:50.959000Z 
      2014-12-20T21:17:50.321000Z 
      red 
      [http://swapi.co/api/films/1/] 
      n/a 
      n/a 
      97 
      http://swapi.co/api/planets/1/ 
      32 
      R5-D4 
      white, red 
      [http://swapi.co/api/species/2/] 
      [] 
      http://swapi.co/api/people/8/ 
      [] 
     
    
      8 
      24BBY 
      2014-12-10T15:59:50.509000Z 
      2014-12-20T21:17:50.323000Z 
      brown 
      [http://swapi.co/api/films/1/] 
      male 
      black 
      183 
      http://swapi.co/api/planets/1/ 
      84 
      Biggs Darklighter 
      light 
      [http://swapi.co/api/species/1/] 
      [http://swapi.co/api/starships/12/] 
      http://swapi.co/api/people/9/ 
      [] 
     
    
      9 
      57BBY 
      2014-12-10T16:16:29.192000Z 
      2014-12-20T21:17:50.325000Z 
      blue-gray 
      [http://swapi.co/api/films/5/, http://swapi.co... 
      male 
      auburn, white 
      182 
      http://swapi.co/api/planets/20/ 
      77 
      Obi-Wan Kenobi 
      fair 
      [http://swapi.co/api/species/1/] 
      [http://swapi.co/api/starships/48/, http://swa... 
      http://swapi.co/api/people/10/ 
      [http://swapi.co/api/vehicles/38/] 
     
  
In [6]:
    
# Let's get character names eye_xolorand skin colors
character_df[['name','skin_color', 'eye_color']]
    
    Out[6]:
  
    
       
      name 
      skin_color 
      eye_color 
     
  
  
    
      0 
      Luke Skywalker 
      fair 
      blue 
     
    
      1 
      C-3PO 
      gold 
      yellow 
     
    
      2 
      R2-D2 
      white, blue 
      red 
     
    
      3 
      Darth Vader 
      white 
      yellow 
     
    
      4 
      Leia Organa 
      light 
      brown 
     
    
      5 
      Owen Lars 
      light 
      blue 
     
    
      6 
      Beru Whitesun lars 
      light 
      blue 
     
    
      7 
      R5-D4 
      white, red 
      red 
     
    
      8 
      Biggs Darklighter 
      light 
      brown 
     
    
      9 
      Obi-Wan Kenobi 
      fair 
      blue-gray 
     
  
In [7]:
    
# let's do the same for the male characters only
character_df[['name','skin_color', 'eye_color']][character_df['gender'] == 'male']
    
    Out[7]:
  
    
       
      name 
      skin_color 
      eye_color 
     
  
  
    
      0 
      Luke Skywalker 
      fair 
      blue 
     
    
      3 
      Darth Vader 
      white 
      yellow 
     
    
      5 
      Owen Lars 
      light 
      blue 
     
    
      8 
      Biggs Darklighter 
      light 
      brown 
     
    
      9 
      Obi-Wan Kenobi 
      fair 
      blue-gray 
     
  
In [ ]:
    
    
Content source: IST256/learn-python
Similar notebooks: