combine_first & update

Python · Combining DataFrames (SQL Joins & Unions)

Theory

Both methods patch one table with values from another, aligning on index and column labels rather than position. a.combine_first(b) returns a new DataFrame that keeps a's values and fills a's missing values (NaN) from b; rows or columns that exist only in b are added. It answers 'use the primary source, fall back to the backup'. A Series version works the same way: s1.combine_first(s2), much like SQL's COALESCE(a.phone, b.phone).

a.update(b) works in place and the other way round: non-missing values in b overwrite a's values at matching labels, NaN in b never overwrites, and rows only in b are ignored. It answers 'apply these corrections'. Since alignment is by label, set the business key as the index first: crm.set_index('cust_id'). update() modifies a and returns None, so do not assign its result. For column-level choice, fillna() with another Series, or np.where, are alternatives.

Why we use it

Data often comes from several partial sources - a CRM missing phone numbers, a correction file with a few fixed emails. combine_first and update merge these by key in one call without writing a join plus many fillna or where steps.

When to use it

Use combine_first to fill gaps from a fallback source, update to overwrite with a list of corrections. Set the key as the index on both frames first. If you need to keep both versions side by side, use merge with suffixes instead.

How it works

  1. Two frames with the same key as index
  2. Fill gaps (primary wins)? Yes: primary.combine_first(backup). No: Apply corrections.
  3. main.update(fixes): non-NaN fixes overwrite matching cells
  4. Check the result; update has changed main in place
  5. Patched DataFrame

Syntax

primary.combine_first(backup)        # fill NaN from backup, new frame
main.update(fixes)                   # overwrite in place with non-NaN fixes
both aligned on index + column labels

Example

crm = crm.set_index('cust_id')
filled = crm.combine_first(erp.set_index('cust_id'))

Key points

Python topics

Getting Started

Variables & Data Types

Operators

Strings

Lists

Tuples

Sets

Dictionaries

Control Flow

Loops

Functions & OOP

Working with Data

Tooling & Errors

File Handling

Exception Handling

NumPy Library

Pandas Basics

Data Cleaning

Combining DataFrames (SQL Joins & Unions)

GroupBy & SQL-Style Analytics

Reshaping & Workflow

Data Visualization

Machine Learning (Scikit-Learn)

Advanced Python & AI

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Python for Data Analysts

From basics to business insights — the complete step-by-step Python roadmap. Click any topic in the sidebar to open it right here.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice Python Online 141 Python questions for data analysts — strings, records, dates, statistics and regex — with hidden tests and instant verdicts. Start practising →
Section {{ sec.num }}

{{ sec.label }}

Python  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs real Python 3.12 in your browser (first run loads the engine)
{{ liveOutput }}
{{ liveError }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic