First argument must be an iterable of pandas objects [Fix]

avatar
Borislav Hadzhiev

Last updated: Apr 12, 2024
3 min

banner

# Table of Contents

  1. First argument must be an iterable of pandas objects
  2. Pass a list containing 2 DataFrames to the pandas.concat() method
  3. Set the ignore_index argument to True
  4. Pass a list of Series objects if concatenating Series

# First argument must be an iterable of pandas objects

The Pandas "TypeError: first argument must be an iterable of pandas objects, you passed an object of type "DataFrame" occurs when you pass a single DataFrame to the pandas.concat() method.

To solve the error, pass a list containing 2 DataFrames to the pandas.concat() method.

Here is an example of how the error occurs.

main.py
import pandas as pd df1 = pd.DataFrame({ 'letter': ['A', 'B'], 'number': [1, 2] }) df2 = pd.DataFrame({ 'letter': ['C', 'D'], 'number': [3, 4] }) df3 = pd.concat(df1) # ⛔️ TypeError: first argument must be an iterable of pandas objects, you passed an object of type "DataFrame" print(df3)

first argument must be iterable of pandas objects

Notice that we passed a single DataFrame to the pandas.concat() method.

# Pass a list containing 2 DataFrames to the pandas.concat() method

To solve the error, pass a list containing 2 DataFrames to the pandas.concat() method.

main.py
import pandas as pd df1 = pd.DataFrame({ 'letter': ['A', 'B'], 'number': [1, 2] }) df2 = pd.DataFrame({ 'letter': ['C', 'D'], 'number': [3, 4] }) df3 = pd.concat([df1, df2]) # 0 A 1 # 1 B 2 # 0 C 3 # 1 D 4 print(df3)

pass list containing 2 dataframes to pandas concat

The code for this article is available on GitHub

The pandas.concat() method takes a sequence of DataFrame objects.

Notice that we passed a list [] containing the two DataFrames to the method.

main.py
# ✅ Correct df3 = pd.concat([df1, df2])

Make sure you're not passing the DataFrames as multiple, individual arguments to the method.

main.py
# ⛔️ Incorrect df3 = pd.concat(df1, df2)

You can specify as many DataFrames as necessary in the list.

main.py
# ✅ Correct df4 = pd.concat([df1, df2, df3])

# Set the ignore_index argument to True

In most cases, you will want to set the ignore_index argument to True.

main.py
import pandas as pd df1 = pd.DataFrame({ 'letter': ['A', 'B'], 'number': [1, 2] }) df2 = pd.DataFrame({ 'letter': ['C', 'D'], 'number': [3, 4] }) df3 = pd.concat([df1, df2], ignore_index=True) # letter number # 0 A 1 # 1 B 2 # 2 C 3 # 3 D 4 print(df3)

set ignore index argument to true

The code for this article is available on GitHub

When the ignore_index argument is set to True, the index is reset in the resulting DataFrame.

Notice that the index goes 0, 1, 2, 3 instead of 0, 1, 0, 1.

# Pass a list of Series objects if concatenating Series

If you're trying to concatenate Series objects, pass them in a list to pandas.concat().

main.py
import pandas as pd s1 = pd.Series(['A', 'B']) s2 = pd.Series(['C', 'D']) s3 = pd.concat([s1, s2], ignore_index=True) # 0 A # 1 B # 2 C # 3 D # dtype: object print(s3)

pass list of series to pandas concat

The code for this article is available on GitHub

You can also specify the axis argument when calling pandas.concat().

When the axis is set to 1, the method will return a DataFrame.

main.py
import pandas as pd s1 = pd.Series(['A', 'B']) s2 = pd.Series(['C', 'D']) df = pd.concat([s1, s2], ignore_index=True, axis=1) # 0 1 # 0 A C # 1 B D print(df)

setting the axis argument to 1

The code for this article is available on GitHub

The axis argument determines the axis to concatenate along.

By default, the argument is set to 0 (index).

Setting the axis argument to 1 means "concatenate along the columns axis".

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.