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

Time:12-01

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)

CodePudding user response:

Your dictionary comprehension doesn't look right. I think you're trying to do the following:

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

processed_dicts = [list(zip([key]*2, somefunction(df))) for (key, df) in df_dict.items()]
df_dict1 = dict([x[0] for x in processed_dicts])
df_dict2 = dict([x[1] for x in processed_dicts])

CodePudding user response:

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()))]
  • Related