Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
ExperiencesCoaching
New
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Start free
Merge Two DataFrames in Python
00:00
5 left

Merge Two DataFrames in Python

EasyPython

Problem

Write a function to merge two dataframes.

Implement merge_dataframes(left, right, key) using an inner join on key. Each dataframe is an array of records, where each record is a map from strings to values. Assume the key exists in every record, is unique within each dataframe, and non-key field names do not overlap. Return an array containing records whose key appears in both inputs, combining fields from both records and preserving the order of matching records from left.

Example: left = [{"id": 1, "name": "A"}], right = [{"id": 1, "team": "X"}] produces [{"id": 1, "name": "A", "team": "X"}].

Constraints: each dataframe contains at most 10^4 records; an input may be empty.

Constraints

  • 0 <= left.length <= 10^4
  • 0 <= right.length <= 10^4
  • Every record contains the specified key
  • The key is unique within each dataframe
  • Non-key field names do not overlap
  • Values used by the key are hashable

Function Signature

def merge_dataframes(left, right, key):
Interviewer

Whenever you're ready, walk me through your approach or go straight to the code. The requirements are in the Question tab.

Sign up free to run or submit. Log in
CodePython 3
Sign up free to run your code. Your draft is saved.Run with a free accountLog inLn 2
Run your code to see test output here.