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

by | Programming, Python, Tips

You can concatenate multiple Series or DataFrames using the pandas.concat() method. If you pass the multiple DataFrames to the concat method without using square brackets, you will raise the TypeError: first argument must be an iterable of pandas objects, you passed an object of type “DataFrame”. The method expects an iterable of pandas objects, not the objects as multiple arguments.

You can solve this error by passing the DataFrames as a list, for example,

import pandas as pd

x = pd.DataFrame()

y = pd.DataFrame()

new_df = pd.concat([x, y])

This tutorial will go through the error in detail and how to solve it with code examples.


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

A TypeError occurs when you perform an operation with an invalid data type. The pandas concat method accepts an iterable of pandas objects. If you pass pandas objects as multiple arguments instead of an iterable, you will raise a TypeError.

Example

Let’s look at an example where we have two DataFrames that we want to concatenate. The first DataFrame contains the height of five subjects in centimetres and the second DataFrame contains the corresponding weight in kilograms. We will attempt to do a column-wise concatenation using pd.concat. We need to set axis = 1 to concatenate along the columns.

import pandas as pd

df1 = pd.DataFrame({'Height':[176, 180, 150, 130, 190]})
df2 = pd.DataFrame({'Weight':[85, 90, 55, 40, 100]})

df3 = pd.concat(df1, df2, axis=1, ignore_index=True)

print(df3)

We also set ignore_index = True to ignore the indexes of the two DataFrames as they do not contain any meaningful information. Let’s run the code to see what happens:

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

We raise the TypeError because the concat method expects an iterable containing pandas objects as its first argument. Instead, it received a single DataFrame df1, and the second DataFrame df2 took the place of the second argument.

Solution

We can solve this error by containing the DataFrames in a list using square brackets. Let’s look at the revised code:

import pandas as pd

df1 = pd.DataFrame({'Height':[176, 180, 150, 130, 190]})
df2 = pd.DataFrame({'Weight':[85, 90, 55, 40, 100]})

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

print(df3)

Let’s run the code to get the result:

   Height  Weight
0     176      85
1     180      90
2     150      55
3     130      40
4     190     100

We successfully concatenated the DataFrames along the column axis.

Solution

Congratulations on reading to the end of this tutorial!

For further reading on TypeErrors involving Pandas, go to the articles:

For further reading on Pandas, go to the article: Introduction to Pandas: A Complete Tutorial for Beginners.

Have fun and happy researching!

Profile Picture
Senior Advisor, Data Science | [email protected] | + posts

Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.

Buy Me a Coffee ✨