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:
JavaScript
x
2
1
[{'a': 'b', 'c': {'c1': 'v1', 'c2': 'v2'}}, {'a': 'b1', 'c': {'c1': 'x1', 'c2': 'x2'}}, {'a': 'b2', 'c': {'c1': 'n1', 'c2': 'n2'}}]
2
Is there a way of modifying structure, so that it will looks like
JavaScript
1
2
1
[{'b': {'c1': 'v1', 'c2': 'v2'}}, {'b1': {'c1': 'x1', 'c2': 'x2'}}, {'b2': {'c1': 'n1', 'c2': 'n2'}}]
2
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 :
JavaScript
1
4
1
def transformation(row):
2
# Where 'correspondingColumn' is the name of your initial column
3
return {row[correspondingColumn]['a']: row[correspondingColumn]['c']}
4
Then you can use apply()
to call this over all the rows of your DataFrame :
JavaScript
1
3
1
# Where 'newCol' is the name of your new column, or if you want to replace the other one, it can be the same
2
my_df['newCol'] = my_df.apply(transformation, axis = 1)
3
Complete example :
JavaScript
1
13
13
1
df = pd.DataFrame({
2
'col':[{'a': 'b', 'c': {'c1': 'v1', 'c2': 'v2'}}]
3
})
4
5
def transformation(row):
6
return {row['col']['a']: row['col']['c']}
7
8
df['newCol'] = df.apply(transformation, axis = 1)
9
10
# Output
11
col newCol
12
0 {'a': 'b', 'c': {'c1': 'v1', 'c2': 'v2'}} {'b': {'c1': 'v1', 'c2': 'v2'}}
13
Update for list of dictionaries :
JavaScript
1
3
1
def transformation(row):
2
return [{elem['a']: elem['c']} for elem in row['col']]
3