pandas合并groupby_遍历Pandas Groupby和合并DataFrames

This seems like it should be straightforward but is stumping me. Really love being able to iterate through the groups of a groupby operation and I am getting the result I want from the groupby but I am unable to merge the final result into one dataframe. So essentially I have the below code which can capture the dataframes in a list but I don't know how to loop through a list of dataframes to make one dataframe:

dfs = []

for name, group in df.groupby('date', sort=False):

dfs.append(pd.DataFrame(pd.DataFrame(list(chain.from_iterable(group['values'])),

columns=['col']).groupby('col').size(),

columns=[name]).reset_index())

and I get the following (just showing dfs[0] and dfs[1] but I have up to dfs[8]:

[ col1 col2

0 val1 val2,

...

col1 col2

0 val1 val2]

...

And I just want to merge them all on the col1 key. In other words the following works great but how do I do it without hardcoding the individual elements in the list and loop through all of them in dfs?

pd.concat([dfs[0], dfs[1]], axis=1, join='outer')

解决方案

Why not just:

pd.concat(dfs, axis=1, join='outer')

你可能感兴趣的:(pandas合并groupby)