Select Page

How to Solve Python AttributeError: ‘DataFrame’ object has no attribute ‘concat’

by | Programming, Python, Tips

A DataFrame is a two-dimensional, mutable tabular data structure like an Excel spreadsheet. If you want to concatenate pandas objects using the method concat(), you must use the built-in pandas method. DataFrame does not have concat as an attribute. If you try to call concat() on a DataFrame object, you will raise the AttributeError: ‘DataFrame’ object has no attribute ‘concat’.

You have to pass the columns to concatenate to pandas.concat() and define the axis to concatenate along.

This tutorial will go through how to solve this error with code examples.


AttributeError: ‘DataFrame’ object has no attribute ‘concat’

AttributeError occurs in a Python program when we try to access an attribute (method or property) that does not exist for a particular object. The part of the error ‘DataFrame’ object has no attribute ‘concat’‘ tells us that the DataFrame object we are handling does not have the concat attribute. The concat() method is a built-in Pandas method which we can access using pandas.concat() or with the common Pandas alias pd, pd.concat().

Example

Let’s look at an example where we have two DataFrames that we want to concatenate. One DataFrame contains two columns, one for pizza names and the other for if the pizza is vegetarian or not. The second DataFrame contains the pizza prices. Let’s look at the data:

import pandas as pd

pizza_names = ['margherita', 'pepperoni', 'four cheeses', 'parmigiana', 'hawaiian', 'marinara']

is_vegetarian = [True, False, True, True, False, True]

df1 = pd.DataFrame(zip(pizza_names, is_vegetarian), columns=['pizza_names', 'is_vegetarian'])

df2 = pd.DataFrame({'prices':[7.99, 8.99, 8.99, 9.99, 9.99, 6.99]})

print(df1)
print(df2)
    pizza_names  is_vegetarian
0    margherita           True
1     pepperoni          False
2  four cheeses           True
3    parmigiana           True
4      hawaiian          False
5      marinara           True
   prices
0    7.99
1    8.99
2    8.99
3    9.99
4    9.99
5    6.99

Next, we will try to concatenate the two DataFrames:

df3 = df1.concat(columns=df2['prices'], axis=1)

print(df3)

Let’s run the code to get the result:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-3-7d12c11cf7a9> in <module>
----> 1 df3 = df1.concat(columns=df2['prices'], axis=1)
      2 print(df3)

~/opt/anaconda3/lib/python3.8/site-packages/pandas/core/generic.py in __getattr__(self, name)
   5581         ):
   5582             return self[name]
-> 5583         return object.__getattribute__(self, name)
   5584 
   5585     def __setattr__(self, name: str, value) -> None:

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

The error occurs because concat is not a DataFrame method; it is a built-in Pandas method.

Solution

We can solve this error by passing the two DataFrames to the built-in Pandas method concat. We specify the axis parameter as 1 so that the method concatenates along the columns. Let’s look at the revised code:

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

print(df3)

print(type(df3))

Let’s run the code to get the result:

   pizza_names  is_vegetarian  prices
0    margherita           True    7.99
1     pepperoni          False    8.99
2  four cheeses           True    8.99
3    parmigiana           True    9.99
4      hawaiian          False    9.99
5      marinara           True    6.99
<class 'pandas.core.frame.DataFrame'>

We successfully concatenated the two DataFrames to get a DataFrame. Note that if at least one of the objects we want to concatenate is a DataFrame, the method returns a DataFrame. When concatenating all Series along the index (axis=0), the method returns a Series. When concatenating along columns (axis=1), the method returns a DataFrame.

Summary

Congratulations on reading to the end of this tutorial. The AttributeError: ‘DataFrame’ object has no attribute ‘concat’ occurs when you try to call the concat method on a DataFrame instead of using the built-in Pandas method.

For further reading on Pandas, go to the articles:

To learn more about Python for data science and machine learning, go to the online courses page on Python for the most comprehensive courses available.

Have fun and happy researching!