Skip to content
Advertisement

Change structure of dictionary in Python Pandas

Is there a way of changing structure of nested dictionary? I have a column in dataframe with many rows of dictionaries, which looks like that:

[{'a': 'b', 'c': {'c1': 'v1', 'c2': 'v2'}}, {'a': 'b1', 'c': {'c1': 'x1', 'c2': 'x2'}}, {'a': 'b2', 'c': {'c1': 'n1', 'c2': 'n2'}}]

Is there a way of modifying structure, so that it will looks like

[{'b': {'c1': 'v1', 'c2': 'v2'}}, {'b1': {'c1': 'x1', 'c2': 'x2'}}, {'b2': {'c1': 'n1', 'c2': 'n2'}}]

without changing actual values?

Advertisement

Answer

You should read about the function apply() in pandas.

You build a function that essentially does your dictionary manipulation :

def transformation(row):
    # Where 'correspondingColumn' is the name of your initial column
    return {row[correspondingColumn]['a']: row[correspondingColumn]['c']}

Then you can use apply() to call this over all the rows of your DataFrame :

# Where 'newCol' is the name of your new column, or if you want to replace the other one, it can be the same
my_df['newCol'] = my_df.apply(transformation, axis = 1)

Complete example :

df = pd.DataFrame({
    'col':[{'a': 'b', 'c': {'c1': 'v1', 'c2': 'v2'}}]
})

def transformation(row):
    return {row['col']['a']: row['col']['c']}

df['newCol'] = df.apply(transformation, axis = 1)

# Output
                                         col                           newCol
0  {'a': 'b', 'c': {'c1': 'v1', 'c2': 'v2'}}  {'b': {'c1': 'v1', 'c2': 'v2'}}

Update for list of dictionaries :

def transformation(row):
    return [{elem['a']: elem['c']} for elem in row['col']]
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement