[英]Convert pandas dataframe columns into nested python dictionary
[英]convert pandas dataframe of multiple columns with NaN to a nested dictionary
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': ...}
對於上述 dataframe detections
將是:
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
和y
) 的值為 NaN 時, detections
具有空值,否則具有相應的值。
我很欣賞任何關於如何在不遍歷每一行的情況下進行detections
的評論?
在time_id
上使用groupby
並應用自定義合並 function merge_dicts
以根據預定義的要求將分組的 dataframe 合並到字典中:
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}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.