Converting a list of nested dictionaries into a set inside of a dataframe

Viewed 90

I've worked with python for quite some time, but for the life of me can't find a solution for converting a column in a dataframe that contains a list of nested dicts into a set containing only values. This first line is an example of a row in my dataframe:

{'source': 'test', 'host': 'server1', 'event': 'metric', 'time': 1297361370, 'fields': {'_value': 0.0, 'metric_name': 'cloud_unit_used', 'enabled': 1, 'aid': 1283541, 'savedEvent': 0, 'accountGroupName': 'AG1', 'testId': 944111, 'testName': 'https://test.com - server1', 'testType': 'http-server', 'interval': 900, **'groups': [{'name': 'Asia Pacific', 'groupId': 11111, 'builtin': 0}, {'name': 'EMEA', 'groupId': 22222, 'builtin': 0}, {'name': 'Switzerland', 'groupId': 33333, 'builtin': 0}, {'name': 'Americas', 'groupId': 44444, 'builtin': 0}]**, 'server': '', 'test_sharing': 'Test Owner', 'url': 'https://server1', 'httpTimeLimit': 5.0, 'pageLoadTimeLimit': '', 'ftpTimeLimit': '', 'agentId': 55555.0, 'agentName': 'AGN1', 'agentType': 'Enterprise', 'countryId': 'GB'}}

The row contains a column labeled groups. Groups is a list of nested dicts:

'groups': [{'name': 'Asia Pacific', 'groupId': 11111, 'builtin': 0}, {'name': 'EMEA', 'groupId': 22222, 'builtin': 0}, {'name': 'Switzerland', 'groupId': 33333, 'builtin': 0}, {'name': 'Americas', 'groupId': 44444, 'builtin': 0}]

I simply want to convert groups into a set containing only the name values in the nested dicts:

'groups': {'Asia Pacific','EMEA','Switzerland','Americas'}

Please note that the number of name values may vary. So in other rows, groups may contain 1 or more names.

Example of resultant row:

{'source': 'test', 'host': 'server1', 'event': 'metric', 'time': 1297361370, 'fields': {'_value': 0.0, 'metric_name': 'cloud_unit_used', 'enabled': 1, 'aid': 1283541, 'savedEvent': 0, 'accountGroupName': 'AG1', 'testId': 944111, 'testName': 'https://test.com - server1', 'testType': 'http-server', 'interval': 900, **'groups': {'Asia Pacific','EMEA','Switzerland','Americas'}**, 'server': '', 'test_sharing': 'Test Owner', 'url': 'https://server1', 'httpTimeLimit': 5.0, 'pageLoadTimeLimit': '', 'ftpTimeLimit': '', 'agentId': 55555.0, 'agentName': 'AGN1', 'agentType': 'Enterprise', 'countryId': 'GB'}}

Can someone please help me find the solution? Very much appreciated all!

2 Answers

This is the general code for this:

For

groups= [{'name': 'Asia Pacific', 'groupId': 11111, 'builtin': 0}, {'name': 'EMEA', 'groupId': 22222, 'builtin': 0}, {'name': 'Switzerland', 'groupId': 33333, 'builtin': 0}, {'name': 'Americas', 'groupId': 44444, 'builtin': 0}]

The code is:

groups=set([i['name'] for i in groups])

print(groups)

{'Americas', 'Switzerland', 'EMEA', 'Asia Pacific'}

Please provide the df constructor if you need the code adjusted for the dataframe

I want to thank you all for the help.. I was able to find a solution which worked:

    for row in merged_df.itertuples():
        merged_df.at[row.Index, 'labels'] = list(set([i['name'] for i in row.groups]))
    merged_df.drop('groups', inplace=True, axis=1)
Related