Home > database >  Change structure of dictionary in Python Pandas
Change structure of dictionary in Python Pandas

Time:06-01

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'}}

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

{'b': {'c1': 'v1', 'c2': 'v2'}}

without changing actual values?

CodePudding user response:

Code:

d = {'a': 'b', 'c': {'c1': 'v1', 'c2': 'v2'}}
dic={}
dic['b'] = d['c']
dic

Output:

{'b': {'c1': 'v1', 'c2': 'v2'}}

CodePudding user response:

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'}}

CodePudding user response:

You can do something like this

dict([d.values()])
  • Related