API RandomUser - Pandas

  • RandomUser is an open-source, free API providing developers with randomly generated users to be used as placeholders for testing purposes.
  • The API can return multiple results, as well as specify generated user details such as gender, email, image, username, address, title, first and last name, and more.
  • More information on RandomUser can be found here.

Case Study

  • Setup RandomUser
  • Generate 10 users
  • Generate names
  • Create a df and save:
    • Full name
    • Gender
    • City
    • State
    • Email
    • DOB
    • Picture

Setup

  • To start using the API you can install the randomuser library running the pip install command.
pip install randomuser
  • Load the necessary libraries.
from randomuser import RandomUser
import pandas as pd

Create RandomUser Object

r = RandomUser()

Generate Users

  • Generate 10 random users and save to rand_list
rand_list = r.generate_users(10)
rand_list
[<randomuser.RandomUser object at 0x000001FCE585A570>, <randomuser.RandomUser object at 0x000001FCE585A5A0>, <randomuser.RandomUser object at 0x000001FCE585A5D0>, <randomuser.RandomUser object at 0x000001FCE585A600>, <randomuser.RandomUser object at 0x000001FCE585A630>, <randomuser.RandomUser object at 0x000001FCE585A660>, <randomuser.RandomUser object at 0x000001FCE585A690>, <randomuser.RandomUser object at 0x000001FCE585A6C0>, <randomuser.RandomUser object at 0x000001FCE585A6F0>, <randomuser.RandomUser object at 0x000001FCE585A720>]

Convert to df

  • If it’s easier to read convert to df
blah = pd.DataFrame(rand_list)
blah
                                                   0
0  <randomuser.RandomUser object at 0x000001FCE58...
1  <randomuser.RandomUser object at 0x000001FCE58...
2  <randomuser.RandomUser object at 0x000001FCE58...
3  <randomuser.RandomUser object at 0x000001FCE58...
4  <randomuser.RandomUser object at 0x000001FCE58...
5  <randomuser.RandomUser object at 0x000001FCE58...
6  <randomuser.RandomUser object at 0x000001FCE58...
7  <randomuser.RandomUser object at 0x000001FCE58...
8  <randomuser.RandomUser object at 0x000001FCE58...
9  <randomuser.RandomUser object at 0x000001FCE58...

Get_full_name & Email

  • The “Get Methods” functions, can generate the required parameters to construct a dataset.
  • For example, to get full name, we call get_full_name() function.
name = r.get_full_name()
name
'Hertha Hauck'
  • Now that we know how to generate full names, let’s loop through all users and generate full name & email for each
# Create a blank list for users
users = []
for user in rand_list:
        users.append({user.get_full_name(), "  ", user.get_email()})
users
[{'gustavo.cortes@example.com', 'Gustavo Cortes', '  '}, {'  ', 'Noemie Fabre', 'noemie.fabre@example.com'}, {'  ', 'devon.perez@example.com', 'Devon Perez'}, {'  ', 'elmer.lynch@example.com', 'Elmer Lynch'}, {'cristina.suarez@example.com', '  ', 'Cristina Suarez'}, {'Juana Hernandez', '  ', 'juana.hernandez@example.com'}, {'Hannah Jones', 'hannah.jones@example.com', '  '}, {'  ', 'liva.nielsen@example.com', 'Liva Nielsen'}, {'Jeppe Poulsen', 'jeppe.poulsen@example.com', '  '}, {'  ', 'Oya Aykaç', 'oya.aykac@example.com'}]

Generate DF

  • We already created the loop to generate and extract some data
  • Let’s create a function get_users() to generate the fields we need and to save the values in a df
  • We pass the function the number of users we want to generate
  • Call the function
# Create a function that receives the number of users needed
def get_users(num):
        
# Create a blank list for users
        users = []
        for user in RandomUser.generate_users(num):
                users.append({"Name":user.get_full_name(),"Gender":user.get_gender(),"City":user.get_city(),"State":user.get_state(),"Email":user.get_email(), "DOB":user.get_dob(),"Picture":user.get_picture()})
                
        return pd.DataFrame(users)
        
  • Call the function using num = 10
  • View DF
df1 = get_users(10)
df1
                Name  ...                                           Picture
0    آیلین سهيلي راد  ...  https://randomuser.me/api/portraits/women/55.jpg
1     Miguel Schmidt  ...    https://randomuser.me/api/portraits/men/20.jpg
2      Obbe Remijnse  ...    https://randomuser.me/api/portraits/men/39.jpg
3    Mathias Schmitt  ...    https://randomuser.me/api/portraits/men/78.jpg
4     Jesse Lehtinen  ...    https://randomuser.me/api/portraits/men/19.jpg
5  Berenice Carvalho  ...  https://randomuser.me/api/portraits/women/24.jpg
6         Miro Polon  ...    https://randomuser.me/api/portraits/men/68.jpg
7   Melodie Mitchell  ...  https://randomuser.me/api/portraits/women/74.jpg
8       Idalina Lima  ...  https://randomuser.me/api/portraits/women/82.jpg
9       Luís Leclerc  ...    https://randomuser.me/api/portraits/men/89.jpg

[10 rows x 7 columns]