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

Convert to df

  • If it’s easier to read convert to df
blah = pd.DataFrame(rand_list)
blah

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
  • 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

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