Merge Map columns in to single column in a pandas dataframe

Viewed 432

I'm trying to merge the rows having dictionary inside the pandas data frame in single row. The output should have latest key values of that columns with all the keys

here is the input dataframe

df = pd.DataFrame([[{'a': 1, 'c': 2}, {'a': 3, 'b': 4}], [{'a': 5, 'b': 6}, {'a': 7, 'b': 8}]], columns=list('AB'))
           A                     B
0   {'a': 1, 'c': 2}    {'a': 3, 'b': 4}
1   {'a': 5, 'b': 6}    {'a': 7, 'b': 8}

expected

           A                     B
0   {'a': 5, 'b': 6, 'c': 2}    {'a': 7, 'b': 8}

Thanks

2 Answers

You can try reduce inside a dict comprehension:

from functools import reduce

d = {c: reduce(lambda x, y:  {**x, **y}, df[c]) for c in df}
d = pd.DataFrame([d.values()], columns=d.keys())

print(d)
                          A                 B
0  {'a': 5, 'c': 2, 'b': 6}  {'a': 7, 'b': 8}

This will give you your expected df:

def consolidate_dicts(input_df):
    # Create consolidated dicts
    dicts = []
    for col in input_df.columns:
        dicts.append({k: v for d in input_df[col] for k, v in d.items()})

    # Replicate original DataFrame with 1 row
    return_df= pd.DataFrame([dicts], columns=input_df.columns)
    return return_df

Edit: The answer from Shubham Sharma is way easier than mine

Related