Search code examples
pythonpandasdictionarydictionary-comprehension

Q: Dict comprehension on a dictionary of pandas dataframes, with a function with multiple outputs


I have a dictionary containing pandas dataframes with corresponding keys:

import pandas as pd
df_dict = pd.read_excel('/directory/data.xlsx', sheet_name=None)

Where the keys are the sheet names of an excel file.

Is it possible to use dict comprehension to create two new dictionaries of pandas dataframes by using a function with two outputs? :

def somefunction(df):
    #Does something
    df1 = df.apply(lambda x: x*2)
    df2 = df.apply(lambda x: x*4)
    return df1, df2

df_dict1, df_dict2 = {key: somefunction(df) for (key, df) in df_dict.items()}
#Does not work..

I know it's possible with the zip(*) keyword when using a list, but it doesn't seem to work with dictionaries

df_dict1, df_dict2 = zip(*{key: somefunction(df) for (key, df) in df_dict.items()})
#Does not work either

Both return

too many values to unpack (expected 2)

Solution

  • You cannot zip dictionnaries, but you can zip their items and build dictionaries back from the item lists. Here you could do:

    [dict(t) for t in zip(*(tuple((key,v)  for v in somefunction(val))
                            for key,val in df_dict.items()))]