Interview Query

Complete Addresses

13
Have you seen this question before?

You’re given two dataframes. One contains information about addresses and the other contains relationships between various cities and states:

Example:

df_addresses

address
4860 Sunset Boulevard, San Francisco, 94105
3055 Paradise Lane, Salt Lake City, 84103
682 Main Street, Detroit, 48204
9001 Cascade Road, Kansas City, 64102
5853 Leon Street, Tampa, 33605

df_cities

city state
Salt Lake City Utah
Kansas City Missouri
Detroit Michigan
Tampa Florida
San Francisco California

Write a function complete_address to create a single dataframe with complete addresses in the format of street, city, state, zip code.

Input:

import pandas as pd

addresses = {"address": ["4860 Sunset Boulevard, San Francisco, 94105", "3055 Paradise Lane, Salt Lake City, 84103", "682 Main Street, Detroit, 48204", "9001 Cascade Road, Kansas City, 64102", "5853 Leon Street, Tampa, 33605"]}

cities = {"city": ["Salt Lake City", "Kansas City", "Detroit", "Tampa", "San Francisco"], "state": ["Utah", "Missouri", "Michigan", "Florida", "California"]}

df_addresses = pd.DataFrame(addresses)
df_cities = pd.DataFrame(cities)

Output:

def complete_address(df_addresses,df_cities) ->

address
4860 Sunset Boulevard, San Francisco, California, 94105
3055 Paradise Lane, Salt Lake City, Utah, 84103
682 Main Street, Detroit, Michigan, 48204
9001 Cascade Road, Kansas City, Missouri, 64102
5853 Leon Street, Tampa, Florida, 33605

Next question: Commercial Break
.....
Python 3.9.6
Loading editor
Use Shift + Enter to run code