[Fixed] attributeerror: ‘dataframe’ object has no attribute ‘append’

If you are a Pandas user and you are trying to alter the rows of your dataframe, the Python interpreter gives the “attributeerror: ‘dataframe’ object has no attribute ‘append’ ” error. Read through this article to solve it.

About the error

pandas version 1.4 and onwards stopped using the append attribute. This leads to the “attributeerror: ‘dataframe’ object has no attribute ‘append'” error in Python. This method discusses some alternate functions to add rows to a data frame.

Methods of resolving the error

The following methods can work in case the append attribute doesn’t work. Also, you can set the pandas version before 1.4 because those versions accept the ‘append’ attribute. Otherwise, it will still give the attributeerror: ‘dataframe’ object has no attribute ‘append’ error.

Using series

The given code shows how you can add columns by appending series to a data frame object.

import pandas as pd

# Create a DataFrame
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})

# Create a Series
s = pd.Series([7, 8], index=['A', 'B'])

# Append the Series to the DataFrame
df = df.append(s, ignore_index=True)

# Print the DataFrame
print(df)

Working with concat() method instead of the append() method

You can go with the concat() function. This helps to append the data frames without giving the attribute error in Python. Pandas version 2.0 and onwards recommend this method.

import pandas as pd

df1 = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
df2 = pd.DataFrame({'C': [7, 8, 9], 'D': [10, 11, 12]})

df = pd.concat([df1, df2], axis=1)
print(df)

Thus, the output will be:

   A  B  C  D
0  1  4  7 10
1  2  5  8 11
2  3  6  9 12

Using a list or dictionary to append

In this case, you need to append the dictionary to the DataFrame. It can be a list, too. Consider this example to know its syntax. It will fix the attributeerror: the ‘dataframe’ object has no attribute ‘append’ error.

import pandas as pd

# Create a DataFrame
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})

# Create a dictionary
data = {'A': 7, 'B': 8}

# Append the dictionary to the DataFrame
df = df.append(data, ignore_index=True)

# Print the DataFrame
print(df)

Reindexing with Series

The reindex() method lets you append series to a data frame without mentioning the index parameter. It itself assigns the correct indexing.

import pandas as pd

# Create a DataFrame
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})

# Create a Series
s = pd.Series([7, 8], index=['C', 'D'])

# Reindex the Series to match the index of the DataFrame
s = s.reindex(df.index)

# Append the Series to the DataFrame
df = df.append(s, ignore_index=False)

# Print the DataFrame
print(df)

Thus, the output Data Frame will be:

   A  B  C  D
0  1  4  nan  nan
1  2  5  nan  nan
2  3  6  nan  nan
3  nan nan  7  8

attributeerror ‘dataframe’ object has no attribute ‘append’ pyspark

If you encounter this error on Pyspark, you can use the union() function to combine two dataframes. You will get one resultant dataframe through this method.

from pyspark.sql import SparkSession

spark = SparkSession.builder.appName("DataFrameAppend").getOrCreate()

df1 = spark.createDataFrame([(1, 2), (3, 4)], ["A", "B"])
df2 = spark.createDataFrame([(5, 6), (7, 8)], ["A", "B"])

df = df1.union(df2)
df.show()

Working with RDD in Pyspark

You can also use the union method here. Otherwise, here’s an alternative approach, too. First, import the SparkContext class. Create a connection to a Spark cluster and then an RDD with the sample data. You can use the parallelize() function for the same. After that, the new row is inserted at the given index. You can utilize the mapPartitions() method to work with the entire partition.

from pyspark import SparkContext

sc = SparkContext()

rdd1 = sc.parallelize([1, 2, 3])
rdd2 = sc.parallelize([4, 5, 6])

def insert_row(rdd, index, row):
    return rdd.mapPartitions(lambda partition: insert(partition, index, row))

def insert(partition, index, row):
    for i, element in enumerate(partition):
        if i == index:
            yield row
        yield element

new_rdd = insert_row(rdd1, 1, 4)

Versions associated with append() attribute

Version 1.4.0 of Python started deprecating this attribute. Then, it was ultimately removed in version 2.0 of Python. If you are using the append() function with the current Python versions, you’ll get attributeerror: ‘dataframe’ object has no attribute ‘append’ error.

attributeerror ‘dataframe’ object has no attribute ‘append_df_to_excel’

The append_df_to_excel can no longer be used. It is better to use the to_excel function for the current versions of python. The mode=’a’ specifies the need to append the dataframe to an Excel file. It will not overwrite the data. Once this has been executed, you can save it.

import pandas as pd

df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})

writer = pd.ExcelWriter('test.xlsx', engine='openpyxl', mode='a')
df.to_excel(writer, sheet_name='Sheet1', startcol=0, startrow=20)
writer.save()

attributeerror ‘dataframe’ object has no attribute ‘append’ databricks

You get the attributeerror: ‘dataframe’ object has no attribute ‘append’ error while working on databricks. This might be because you have created a Pandas DataFrame object. However, in data bricks, you require a spark data frame object for writing. This will combine multiple dataframes.

spark.createDataFrame(df).write.saveAsTable("dashboardco.AccountList")

How .append words in a list vs. in a dataframe

When you are using the append() function on a list, it is going to add a single element. This addition occurs at the end. But in the case of a data frame, the append() function adds one new row or multiple rows or rows of a different data frame to your data frame. A data frame can be one dictionary-like object, a list of dictionaries, or a complete data frame.

Adding to this, in the case of a list, the append function alters the original list, but it creates a copy while working on the data frame. When it comes to performance assessment, a list append will work faster because that involves a smaller dataset. Consider this comparison table to know more about the functioning of a list and a data frame.

FeatureListDataFrame
Data StructureOne-dimensional, ordered collectionTwo-dimensional, labeled columns and rows
Data TypeHomogeneous, any typeHeterogeneous, different types per column
Appending with .appendCan be slower for single-element appendsAdds row(s) based on dictionary-like object(s) or concatenates DataFrames
In-place modificationYesNo
PerformanceFaster for small datasetsCan be slower for single element appends
Use casesSmall, homogeneous data, temporary data structuresLarge, structured data, data analysis

Other tips while working with pandas data frame concatenation

These tips may prove to be quite useful while working with a panda data frame. You can utilize these while resolving the attributeerror: the ‘dataframe’ object has no attribute ‘append’ error.

  • The concat() function can work for larger Data Frames. It doesn’t bother about different numbers of rows. It will concatenate all of them.
  • Series are quite useful if you want to add one row only.
  • The data frame should have the same data types as the rows you are planning to add.
  • Avoid data that has missing values.

FAQs

Why was the .append() method removed?

This was because it couldn’t handle large datasets.

Conclusion

This article delves into the intricacies of the “attributeerror: ‘dataframe’ object has no attribute ‘append’ ” error. It elaborates on the causes of the error and discusses how a user can append rows to a dataframe in Python. In addition to this, you must have discovered an alternative to the ‘append_df_to_excel’ attribute.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments