Skip to content
Happy Programming Guide
Start learning
Python

AttributeError: ‘DataFrame’ Object Has No Attribute ‘append’

pandas removed DataFrame.append in version 2.0. Here is the replacement, why building a DataFrame in a loop was always the wrong approach, and the faster pattern.

Two monitors on a developer desk

AttributeError: 'DataFrame' object has no attribute 'append' means you are on pandas 2.0 or later. The method was deprecated in 1.4 and removed in 2.0. The direct replacement is pd.concat() — but if you were calling append inside a loop, there is a much faster pattern that was always the better answer.

The direct replacement#

Python
import pandas as pd

a = pd.DataFrame({"x": [1, 2]})
b = pd.DataFrame({"x": [3, 4]})

# Old - removed in pandas 2.0
combined = a.append(b)

# New
combined = pd.concat([a, b], ignore_index=True)
Terminal
python -c "import pandas; print(pandas.__version__)"

ignore_index=True matters. Without it you keep both original indexes, so the result has index values 0, 1, 0, 1 — and any later lookup by index returns two rows where you expected one.

Why appending in a loop was always wrong#

Python
# The pattern people are usually replacing
df = pd.DataFrame()
for row in rows:
    df = df.append(row, ignore_index=True)      # or now, pd.concat

Every call copies the entire DataFrame. Appending a thousand rows means a thousand copies of a growing table — the total work grows with the square of the row count. On ten thousand rows this can take minutes.

Collect first, build once:

Python
records = []

for row in rows:
    records.append({"name": row.name, "amount": row.amount})

df = pd.DataFrame(records)

A Python list append is cheap and the DataFrame is constructed in one pass. On a realistic dataset this is commonly a hundred times faster.

Adding a single row#

Python
df = pd.DataFrame({"name": ["Ana"], "score": [90]})

# By label - fine for one or two rows
df.loc[len(df)] = {"name": "Sam", "score": 85}

# With concat
new_row = pd.DataFrame([{"name": "Kim", "score": 88}])
df = pd.concat([df, new_row], ignore_index=True)

df.loc[len(df)] is the shortest form and works when the index is a clean integer range. If the index is anything else — dates, strings, or gaps after filtering — it will overwrite an existing row rather than adding one.

Combining many frames#

Python
frames = [pd.read_csv(path) for path in paths]
combined = pd.concat(frames, ignore_index=True)
Python
# Keep track of where each row came from
combined = pd.concat(
    {path.stem: pd.read_csv(path) for path in paths},
    names=["source"],
).reset_index(level="source")

Passing a dictionary gives you a labelled index, which answers “which file did this row come from” without adding a column by hand.

Mismatched columns#

Python
a = pd.DataFrame({"x": [1], "y": [2]})
b = pd.DataFrame({"x": [3], "z": [4]})

print(pd.concat([a, b], ignore_index=True))
Output
     x    y    z
0  1.0  2.0  NaN
1  3.0  NaN  4.0

Columns are unioned and gaps filled with NaN. That is often what you want — and if it is not, it is a silent data problem. Check first:

Python
if set(a.columns) != set(b.columns):
    print("only in a:", set(a.columns) - set(b.columns))
    print("only in b:", set(b.columns) - set(a.columns))

To keep only the shared columns:

Python
combined = pd.concat([a, b], join="inner", ignore_index=True)

Side by side rather than stacked#

Python
pd.concat([a, b], axis=1)       # columns, aligned on the index

For joining on a key rather than the index, concat is the wrong tool — use merge:

Python
result = orders.merge(customers, on="customer_id", how="left")

Series.append is gone too#

Python
s1 = pd.Series([1, 2])
s2 = pd.Series([3, 4])

combined = pd.concat([s1, s2], ignore_index=True)

The same removal, the same replacement.

A version-safe helper#

Python
import pandas as pd


def stack(frames, ignore_index=True):
    """Combine DataFrames on any pandas version."""
    frames = [f for f in frames if f is not None and not f.empty]
    if not frames:
        return pd.DataFrame()
    return pd.concat(frames, ignore_index=ignore_index)

Filtering out empty frames is worth doing: concatenating an empty DataFrame used to produce a FutureWarning about dtype changes, and dropping them avoids the question entirely.

Building efficiently#

Python
# From a generator, with no intermediate list
df = pd.DataFrame(
    ({"id": row.id, "total": row.qty * row.price} for row in source)
)

# From a database, in chunks
chunks = pd.read_sql("SELECT * FROM orders", conn, chunksize=10_000)
df = pd.concat(chunks, ignore_index=True)

# From many files
df = pd.concat(
    (pd.read_csv(p) for p in Path("data").glob("*.csv")),
    ignore_index=True,
)

Each of these builds the DataFrame once from an iterable, which is the pattern to reach for whenever you find yourself thinking about appending.

Questions people ask#

Which version removed it?

Deprecated in pandas 1.4, removed in 2.0. Between those versions it worked and emitted a FutureWarning.

Why was it removed rather than fixed?

Because it encouraged a pattern that is inherently slow. A DataFrame is a column-oriented structure; adding rows one at a time works against how it is stored. Removing the method pushes people towards building once.

Does concat modify the originals?

No. It returns a new DataFrame, and the inputs are unchanged.

Should I downgrade pandas instead?

No. The change is small and the replacement is better. Downgrading leaves you on an old version while everything around it moves on.

Where to go next#

Python and data science, with pandas done properlyRead next

Keep reading

Keep going — pick your next guide

The fastest way to improve is to read one guide, then build the thing it describes. Start with the basics, or jump straight to a project.

Ask a question or share what worked

Your email address will not be published. Required fields are marked *