python - 将具有 NaN 的多列 Pandas 数据框转换为嵌套字典

标签 python pandas dataframe dictionary nested

                         object_id                  time_id                      class         x       y
0  3db53411-c23b-49ec-8635-adc4e3ee2895  5G21A6P01L4100029:1570754223950071         NaN       NaN      NaN
1  3cea3cdc-883e-48d7-83de-e485da2e085a  5G21A6P01L4100029:1570754223950071        PERSON   528.868  2191.747
2  fc87a12f-a76a-4273-a712-6f56afc042c6  5G21A6P01L4100029:1570754223950071          CAR   512.238  2192.744
3  4edb4e32-0345-4f85-a4b1-e60903368fed  5G21A6S09K40039EX:1565470602550590          NaN      NaN       NaN
4  cd68a1d0-2470-4096-adb1-201017aadc9e  5G21A6S09K40039EX:1565470602550590         PERSON -1305.968 -2423.231

我有一个嵌套字典 detections 具有以下架构

detections = defaultdict(dict)
detections[key:time_id][key:object_id] = {'class_text':... , 'x': ..., 'y': ...}

对于上述数据框,检测将是:

detections[5G21A6P01L4100029:1570754223950071] = 
{
`3db53411-c23b-49ec-8635-adc4e3ee2895`: {},
'3cea3cdc-883e-48d7-83de-e485da2e085a': {'class_text': 'PERSON', 'x': 528.8, 'y': 2191.7}, 
'fc87a12f-a76a-4273-a712-6f56afc042c6': {'class_text': 'CAR', 'x': 512.2, 'y': 2192.7}}
}

detections["5G21A6S09K40039EX:1565470602550590"] = 
{
`4edb4e32-0345-4f85-a4b1-e60903368fed`: {},
'cd68a1d0-2470-4096-adb1-201017aadc9e': {'class_text': 'PERSON', 'x': -1305.968, 'y': -2423.23}
}

当 (class, x and y) 的值为 NaN 时,

detections 为空值,否则它具有相应的值。

对于如何在不对每一行进行循环的情况下进行检测的任何评论,我都很感激?

最佳答案

time_id 上使用 groupby 并应用自定义合并函数 merge_dicts 根据预定义的要求将分组数据帧合并到字典中:

def merge_dicts(s):
    s = s.set_index('object_id')[['class', 'x', 'y']]
    return s.agg(lambda x: {} if x.isna().all() else dict(**x), axis=1).to_dict()

detections = df.groupby('time_id').apply(merge_dicts).to_dict()

结果:

print(detections)

{
    '5G21A6P01L4100029: 1570754223950071': 
    { 
        '3db53411-c23b-49ec-8635-adc4e3ee2895': {},
        '3cea3cdc-883e-48d7-83de-e485da2e085a': {'class': 'PERSON', 'x': 528.868, 'y': 2191.7470000000003},
        'fc87a12f-a76a-4273-a712-6f56afc042c6': {'class': 'CAR', 'x': 512.238, 'y': 2192.744}
    },
    '5G21A6S09K40039EX: 1565470602550590': 
    {
        '4edb4e32-0345-4f85-a4b1-e60903368fed': {},
        'cd68a1d0-2470-4096-adb1-201017aadc9e': {'class': 'PERSON', 'x': -1305.968, 'y': -2423.231}
    }
}

关于python - 将具有 NaN 的多列 Pandas 数据框转换为嵌套字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63166521/

相关文章:

python - 哪些领域涉及提取具有相似特征的单词?

python - 在给定的时间间隔内循环或包装颜色图

python - 将不规则时间序列转换为python pandas中的每小时数据

python - 如何在 Python 中连接列表和数据框以创建字典

python - Keras 值错误 : This model has never been called

python - wxpython中的多行复选框

python - Pandas :选择每组中的前几行

python - 从 pandas 数据框中指定 x 轴图上的日期

Python - 尝试存储列表数组时陷入循环

python - 通过 pandas read_html 获取 HTML 表将不起作用