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?
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']]
Code:
d = {'a': 'b', 'c': {'c1': 'v1', 'c2': 'v2'}}
dic={}
dic['b'] = d['c']
dic
Output:
{'b': {'c1': 'v1', 'c2': 'v2'}}
你可以做这样的事情
dict([d.values()])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.