【问题标题】:Converting a list of nested dictionaries into a set inside of a dataframe将嵌套字典列表转换为数据框内的集合
【发布时间】:2021-01-15 10:21:35
【问题描述】:

我使用 python 已经有一段时间了,但在我的一生中,我找不到将包含嵌套字典列表的数据框中的列转换为仅包含值的集合的解决方案。第一行是我的数据框中的一行示例:

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

该行包含标记为组的列。 Groups 是嵌套字典的列表:

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

我只是想将组转换为仅包含嵌套字典中的名称值的集合:

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

请注意,名称值的数量可能会有所不同。因此,在其他行中,组可能包含 1 个或多个名称。

结果行示例:

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

有人可以帮我找到解决方案吗?非常感谢大家!

【问题讨论】:

  • 您希望该集合仅包含字典的值吗?还是键和值?还是只是钥匙?
  • 提供了一个list of dict 对象(groups),s = {d['name'] for d in groups} 将从每个dict 中提取与键'name' 关联的所有dict 值到@987654332 @分配给变量s。使用提供的示例进行测试并获得预期的输出。不是 100% 关于如何在 df 更新的上下文中最好地实现这一点 - df.apply()?
  • 您好,感谢您对 Mushif 的回复……只有价值观。

标签: python list dataframe dictionary set


【解决方案1】:

感谢大家的帮助。我找到了一个有效的解决方案:

    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)

【讨论】:

    【解决方案2】:

    这是通用代码:

    对于

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

    代码是:

    groups=set([i['name'] for i in groups])
    
    print(groups)
    
    {'Americas', 'Switzerland', 'EMEA', 'Asia Pacific'}
    

    如果您需要为数据框调整代码,请提供 df 构造函数

    【讨论】:

    • 你好弓箭手。我想花点时间感谢你。这很有帮助。 Python 版本是 Anaconda 3.7,我正在使用 pandas from_dict 创建数据框。希望这就是您要找的东西?
    • 在数据框中仍然存在问题
    • 尝试了几种不同的方法来尝试将数据放入数据框中,但到目前为止都没有成功:对于索引,merged_df.iterrows() 中的行:groups = row.groups groups=set([ i['name'] for i in groups]) mapped_df.loc[index, 'groups'] = groups for index, row in mapped_df.iterrows(): groups = row.groups region=set([i['name' ] for i in groups]) 用于索引,merged_df.iterrows() 中的行:groups = row.groups region = set([i['name'] for i in groups]) mapped_df.loc[index,'regions'] = 地区
    • 嗨 mojo 你有数据框格式的数据吗?构建数据框的最有效方法(假设您的数据已经在字典中,如下所示:df=pd.DataFrame(your_dict) 或者您可以手动添加它,如下所示:df=pd.DataFrame({'a':somedata, 'b':someotherdata, ...}) 其中“a”、“b”等将是您的列名
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-09-27
    • 2014-05-10
    • 1970-01-01
    • 2020-03-31
    • 2020-09-03
    • 2020-09-23
    相关资源
    最近更新 更多