我正在尝试将嵌套词典的字典转换为数据框,然后将其转换为csv。
我只想从主键properties
中提取所有键和值。
dict_keys(['addedAt', 'vid', 'canonical-vid', 'merged-vids', 'portal-id', 'is-contact', 'profile-token', 'profile-url', 'properties', 'form-submissions', 'identity-profiles', 'merge-audits'])
在属性中,值也存储为键值对。这是它的摘要:
dict_items([('firstname', {'value': 'John'}), ('associatedcompanyid', {'value': '54321'}), ('hs_analytics_last_url', {'value': 'https://website.com/contactus/'}), ('more keys', {'value': 'string'})
以下代码似乎可以完成任务。但它只会为一行创建一个数据框。
dfobj = pd.DataFrame(dict([ (k,pd.Series(v)) for k, v in contact_dict['properties'].items() ]))
我将如何创建一个for循环,以从嵌套字典中获取所有键,值以及值内的实际值,并将其转换为数据框?
挑战在于,并非每个记录在properties
下都具有相同的数组长度。因此,我一直收到此错误ValueError: arrays must all be same length
。
这是我运行的代码:
df = pd.DataFrame(contact_dict)
df1 = df.transpose()
这是我收到的全部错误。第107行是df = pd.DataFrame(contact_dict)
所在的位置。任何提示都将不胜感激:
File "C:\Users\alexs\Desktop\pythonenvs\get_hubspot_2.py", line 107, in <module>
df = pd.DataFrame(contact_dict)
File "C:\Python\Python38\lib\site-packages\pandas\core\frame.py", line 411, in __init__
mgr = init_dict(data, index, columns, dtype=dtype)
File "C:\Python\Python38\lib\site-packages\pandas\core\internals\construction.py", line 257, in init_dict
return arrays_to_mgr(arrays, data_names, index, columns, dtype=dtype)
File "C:\Python\Python38\lib\site-packages\pandas\core\internals\construction.py", line 77, in arrays_to_mgr
index = extract_index(arrays)
File "C:\Python\Python38\lib\site-packages\pandas\core\internals\construction.py", line 368, in extract_index
raise ValueError("arrays must all be same length")
ValueError: arrays must all be same length```