简体   繁体   中英

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!

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM