gtomer
gtomer

Reputation: 6564

How to explode a dict column into a new dataframe

I have the following sample dataframe:

df = pd.DataFrame( [[20, 30, [{"ab":"1", "we":"2", "as":"3"}, {"ab":"4", "we":"5", "as":"6"}],"String"]],
            columns=['A', 'B', 'C', 'D'])

I want to explode the 'C' column into a new dataframe. It is formatted as dict but its type is an object. Tried converting it into dict using ast.literal_eval but while doing that on the full dataframe I got an EOF error.

Desired output of the new dataframe:

ab    we     as
1     2      3
4     5      6

Upvotes: 1

Views: 219

Answers (1)

Vishnudev Krishnadas
Vishnudev Krishnadas

Reputation: 10960

Use apply with pd.Series

df.C.apply(pd.Series)

Output

  ab we as
0  1  2  3

If the column type is object, use this

df.C.apply(lambda x: pd.Series(literal_eval(x)))

If you have invalid dict objects

def explode_dict(row):
    try:
        return pd.Series(literal_eval(row))
    except Exception as e:
        print('Error Converting', row)
        return np.nan

df.C.apply(explode_dict)

If the column is a list of dict, use,

df.C.explode().apply(explode_dict)

Upvotes: 2

Related Questions