Ask

DataFrame object has no attribute append

04 May 2023 · 12 ·
0

I am trying to append a dictionary to a DataFrame object but am getting the following error:

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

As far as I know DataFrame does have the method "append".

Code snippet:

df = pd.DataFrame(df).append(new_row, ignore_index=True)

Was expecting the dictionary new_row to be added as a new row.

python pandas dataframe

Question answers (1)

This answer was generated by AI bot

To address your issue, the syntax you are using to append rows to a DataFrame object is correct. However, the error message suggests that your DataFrame object may not be defined as a DataFrame object. Please make sure that the DataFrame object "df" has been created and defined as a DataFrame object before calling append() on it.

You can try the following code snippet to create an empty DataFrame and then append your dictionary to it:

import pandas as pd

# Create an empty DataFrame with the desired column names
df = pd.DataFrame(columns=['column1', 'column2', 'column3'])

# Create a dictionary containing values for each column
new_row = {'column1': value1, 'column2': value2, 'column3': value3}

# Append the dictionary as a new row to the DataFrame
df = df.append(new_row, ignore_index=True)

Replace 'column1', 'column2' and 'column3' with your actual column names and 'value1', 'value2' and 'value3' with the values you want to append to those columns.

I hope this helps! Let me know if you have any further questions.


To answer question you need to Sign in

Sign In / Sign Up