Reputation: 313
I have the below list containing dictionaries, which themselves contain dataframes, that I would like to merge.
ls = [{'varID': 101, 'varDesc': 'Description of variable 101', 'values': df101},
{'varID': 102, 'varDesc': 'Description of variable 102', 'values': df102},
{'varID': 103, 'varDesc': 'Description of variable 103', 'values': df103}]
Where df101, df102, and df103 are all x by 1 dataframes in shape with date values as the index (named 'refPer'), and the column name of 'values' in all the dataframes. Note: that x is not the same size for all the dateframes.
I would like to merge the dataframes and have attempted the following:
mergedDF = reduce(lambda x,y: pd.merge(x.get('values'),y.get('values'), how = 'outer', on = 'refPer'), ls)
However, I get the following error:
ValueError: can not merge DataFrame with instance of type <class 'NoneType'>
Any help would be appreciated.
Thank you in advance
Upvotes: 0
Views: 51
Reputation: 10580
You can use concat
to join Dataframes of unequal length. You can also provide the parameter keys
to give a name to each dataframe you are concatenating. You'll create a multiindex, but you can drop the original level if you don't want it:
df = pd.concat([x['values'] for x in ls], axis=1, keys=[x['varID'] for x in ls])
df.columns = df.columns.droplevel(1) # removes 'original' level
Upvotes: 1