我有一个很大的defaultdict (list)
,其中包含大量的键和值。列表的大小也是可变的。
我想知道在将嵌套结构写入Pandas DataFrame
之前,如何解开嵌套结构的Python方式。我想在DataFrame
的(row,col)中使用一个值。
代码
from collections import defaultdict
import pandas as pd
dict_one_needle = defaultdict(list)
new_default_dict = defaultdict(list)
needle_data = []
# some mock-up data
dict_one_needle['PatientID'].append('999')
dict_one_needle['PatientName'].append('another')
dict_one_needle['LesionNr'].append('two')
dict_one_needle['NeedleNr'].append(None)
# more mock-up data
for idx_s in range(0, 3):
dict_one_needle['PatientID'].append('123')
dict_one_needle['PatientName'].append('john doe')
dict_one_needle['LesionNr'].append(1)
dict_one_needle['NeedleNr'].append(2)
# append to list
needle_data.append(dict_one_needle)
# some mock-up data
new_default_dict['PatientID'].append(0)
new_default_dict['PatientName'].append('rs')
new_default_dict['LesionNr'].append(0)
new_default_dict['NeedleNr'].append(0)
# append again to list
needle_data.append(new_default_dict)
# unpack the nested list of defaultdict(list)
new_dict = defaultdict(list)
for needles in needle_data:
# take each key of the dictionary, each value
for keys, vals in needles.items():
for val in vals:
new_dict[keys].append(val)
#convert to DataFrame
df = pd.DataFrame(new_dict)