How to Fix: first argument must be an iterable of pandas objects, you passed an object of type “DataFrame”


One common error you may encounter when using Python is:

TypeError: first argument must be an iterable of pandas objects, you passed an object
           of type "DataFrame"

This error usually occurs when you attempt to use the concat() function to append two pandas DataFrames together without wrapping the DataFrame names in brackets.

The following example shows how to resolve this error in practice.

How to Reproduce the Error

Suppose we have the following two pandas DataFrames:

import pandas as pd

#create first DataFrame
df1 = pd.DataFrame({'x': [25, 14, 16, 27, 20,15, 14],
                    'y': [5, 7, 7, 5, 7, 6, 9],
                    'z': [8, 8, 10, 6, 6, 9, 6]})

print(df1)

    x  y   z
0  25  5   8
1  14  7   8
2  16  7  10
3  27  5   6
4  20  7   6
5  15  6   9
6  14  9   6

#create second DataFrame 
df2 = pd.DataFrame({'x': [58, 60, 65],
                    'y': [14, 22, 23],
                    'z': [9, 12, 19]})

print(df2)

    x   y   z
0  58  14   9
1  60  22  12
2  65  23  19

Now suppose we attempt to use the concat() function to append the two DataFrames into one DataFrame:

#attempt to append two DataFrames together
combined = pd.concat(df1, df2, ignore_index=True)

#view final DataFrame
print(combined)

TypeError: first argument must be an iterable of pandas objects, you passed an object
           of type "DataFrame"

We receive an error because we failed to wrap the DataFrame names in brackets within the concat() function.

How to Fix the Error

The way to resolve this error is to simply wrap the DataFrame names in a bracket within the concat() function as follows:

#append two DataFrames together
combined = pd.concat([df1, df2], ignore_index=True)

#view final DataFrame
print(combined)

    x   y   z
0  25   5   8
1  14   7   8
2  16   7  10
3  27   5   6
4  20   7   6
5  15   6   9
6  14   9   6
7  58  14   9
8  60  22  12
9  65  23  19

Notice that we’re able to successfully combine the two DataFrames without any error this time.

Additional Resources

The following tutorials explain how to fix other common errors in Python:

How to Fix in Python: ‘numpy.ndarray’ object is not callable
How to Fix: TypeError: ‘numpy.float64’ object is not callable
How to Fix: Typeerror: expected string or bytes-like object

Leave a Reply

Your email address will not be published.