How can I fix the error “First argument must be an iterable of pandas objects, you passed an object of type ‘DataFrame'”?

How can I fix the error “First argument must be an iterable of pandas objects, you passed an object of type ‘DataFrame'”?

The error “First argument must be an iterable of pandas objects, you passed an object of type ‘DataFrame'” occurs when a non-iterable object, such as a DataFrame, is passed as the first argument in a function that requires an iterable of pandas objects. To fix this error, you will need to ensure that the first argument is a proper iterable, such as a list or a tuple, containing pandas objects such as DataFrames or Series. Additionally, it is important to check the documentation of the function to ensure that the correct data type is being passed as the first argument.

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.

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

Cite this article

stats writer (2024). How can I fix the error “First argument must be an iterable of pandas objects, you passed an object of type ‘DataFrame'”?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-fix-the-error-first-argument-must-be-an-iterable-of-pandas-objects-you-passed-an-object-of-type-dataframe/

stats writer. "How can I fix the error “First argument must be an iterable of pandas objects, you passed an object of type ‘DataFrame'”?." PSYCHOLOGICAL SCALES, 27 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-fix-the-error-first-argument-must-be-an-iterable-of-pandas-objects-you-passed-an-object-of-type-dataframe/.

stats writer. "How can I fix the error “First argument must be an iterable of pandas objects, you passed an object of type ‘DataFrame'”?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-fix-the-error-first-argument-must-be-an-iterable-of-pandas-objects-you-passed-an-object-of-type-dataframe/.

stats writer (2024) 'How can I fix the error “First argument must be an iterable of pandas objects, you passed an object of type ‘DataFrame'”?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-fix-the-error-first-argument-must-be-an-iterable-of-pandas-objects-you-passed-an-object-of-type-dataframe/.

[1] stats writer, "How can I fix the error “First argument must be an iterable of pandas objects, you passed an object of type ‘DataFrame'”?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can I fix the error “First argument must be an iterable of pandas objects, you passed an object of type ‘DataFrame'”?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top