Search code examples
pythonpandasdictionaryconcatenation

Using concat for a dictionary I get the error: first argument must be an iterable of pandas objects, you passed an object of type "DataFrame"


I have a dictionary with dataframes, that looks like this:

dataframes = {'Df_20100101': DataFrame, 'Df_20100102': DataFrame, 'Df_20100103': DataFrame}

All dataframes have the same variables (Price, Volume and Date) and the same Index. I want to put all the different dataframes into 1 dataframe. I use the following code:

df = pd.concat([pd.concat(v,ignore_index=True) for k,v in dataframes.items()])

However, I get an error: the first argument must be iterable of pandas objects, you passed an object of type "DataFrame".

Is it because all the variables have the same Index?

Can anyone help me out?

Thank you!


Solution

  • You're calling pd.concat on individual dataframes in your dictionary (you have nested pd.concat() calls). Instead, you just want a list comprehension to gather the dataframes into a single list and call .concat() on that:

    df = pd.concat([v for _, v in dataframes.items()], ignore_index=True)
    

    for k, v is perfectly valid too, but it's customary to use _ for values that you're going to throw away, and you don't use the key here.

    You could also just use .values():

    df = pd.concat(dataframes.values(), ignore_index=True)