Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Pandas · Module

df.copy

Creates a new DataFrame from the caller’s data and indices. By default it copies them; deep=False creates a shallow copy, with Copy-on-Write protecting writes from affecting the other object.

How it works

Call copy() to create another DataFrame with copied data and indices by default. Passing deep=False creates a shallow copy that initially shares references, while Copy-on-Write prevents writes through either object from changing the other.

Syntax

DataFrame.copy(deep=True)

Parameters

NameTypeDescription
deepbool, default TrueControls whether the DataFrame's data and indices are copied. True makes a deep copy; False copies only references.

Returns

DataFrame; the returned object type matches the caller.

Key points

  • DataFrame.copy creates a separate DataFrame object based on the caller's data and index.
  • The default deep=True duplicates the DataFrame's data and indices.
  • Edits to a normal deep copy do not propagate back to the source DataFrame.
  • deep=False makes a shallow DataFrame copy that reuses references instead of duplicating data and index storage.
  • Under Copy-on-Write, modifying a shallow copy lazily materializes independent data at write time.

Common mistakes

  • deep=True does not recursively copy Python objects stored inside cells; nested mutable objects can still be shared.
  • A shallow copy does not independently duplicate the data or index, so do not use it when explicit physical separation is required.
  • With Copy-on-Write enabled, shallow-copy writes trigger a new data copy rather than mutating the other object.

Example: Independent DataFrame copy

import pandas as pd

original = pd.DataFrame({"score": [10, 20]})
copy = original.copy()
copy.loc[0, "score"] = 99

print(original.loc[0, "score"])  # 10
print(copy.loc[0, "score"])      # 99

The default copy has independent data and index storage, so changing its scalar value does not change the original DataFrame.

When to use it

Safe independent editing

You need to modify a derived DataFrame without changing the source.

Shallow copying

You want a lightweight object sharing data and index references initially.

Copy-on-Write workflows

You need to isolate writes while avoiding eager duplication.

Interview questions

QWhat is the default behavior of DataFrame.copy()?

copy() returns a new object containing copies of the caller's data and indices by default because deep defaults to True. Changes to the copied data or indices are therefore not reflected in the original.

Follow-up: What does deep=True mean for objects stored in cells? It copies the container data but only copies references to nested Python objects, not those objects recursively.

QHow does deep=False differ from the default?

deep=False creates a new DataFrame while copying neither the data nor the index; it copies references to them instead. The returned object still has the same type as its caller.

Follow-up: How does Copy-on-Write alter shallow-copy behavior? A write to either object causes the affected data to be copied, preventing the other object from observing that mutation.

QWhat does DataFrame.copy() return?

The method returns a Series or DataFrame whose type matches the caller. For a DataFrame, this means copy() produces another DataFrame.

Follow-up: Why can an index's underlying array remain shared even with deep=True? Index objects are immutable, so sharing their underlying data is safe and avoids an unnecessary copy.

  • DataFrame.astype
  • DataFrame.iloc
  • DataFrame.loc
  • DataFrame.copy

Source: pandas.DataFrame.copy. Checked 2026-10-15.