How do I fix the 'DataFrame' object has no attribute 'append' error when adding a new row?

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.