I’m trying to add a dictionary as a new row to a DataFrame, but I keep getting the error:
AttributeError: ‘DataFrame’ object has no attribute ‘append’
I thought append() was valid for DataFrames, but it seems this no longer works. Here’s the line I’m using:
df = pd.DataFrame(df).append(new_row, ignore_index=True)
This used to work earlier, so why am I seeing ‘DataFrame’ object has no attribute ‘append’ now? Has the method been deprecated in recent versions of pandas? What’s the current recommended way to add a row to a DataFrame?
Ah, you’ve probably run into this because the append()
method was deprecated in pandas 1.4 and fully removed in pandas 2.0. So, you’re right to be getting that error now. The new way to do this is using pd.concat()
instead. Here’s how you can add a row with it:
df = pd.concat([df, pd.DataFrame([new_row])], ignore_index=True)
This approach does exactly what append()
used to, but it’s far more efficient, especially when you need to add multiple rows. It’s a small tweak, but it’ll save you compatibility headaches down the line.
Right, and if this is happening inside a loop or in a batch process, frequent use of concat
can be a performance bottleneck. Instead, it’s better to collect all your rows in a list and then use concat()
once at the end. Here’s an example:
rows = [existing_row1, existing_row2, new_row]
df = pd.DataFrame(rows)
Or even store the new rows in a list and convert them to a DataFrame later:
new_data = pd.DataFrame(new_rows_list)
df = pd.concat([df, new_data], ignore_index=True)
This method avoids the costly reallocation that comes with repeated appending. It’s a big performance win, especially with larger datasets.
You can also take a more straightforward approach if you’re only adding a single row. It’s not as fancy as concat()
, but it’s definitely efficient for small-scale tasks. Use .loc
to add your row directly like this:
df.loc[len(df)] = new_row
This method is super readable and doesn’t require append() or concat(). It works great when you’re dealing with smaller or medium-sized dataframes, and it’s really convenient for scripting or interactive work.