我有一个类似清单的字典-
from collections import defaultdict
defaultdict(list,
{'row1': ['Affinity'],
'row2': ['Ahmc',
'Garfield',
'Medical Center'],
'row3': ['Alamance','Macbeth'],
'row4': [],
'row5': ['Mayday']})
我想将其转换为数据帧。 输出应类似于-
ID SYN1 SYN2 SYN3 SYN4 SYN5
row1 Affinity
row2 Ahmc Garfield Medical Center
row3 Alamance Macbeth
row4
row5 Mayday
答案 0 :(得分:6)
collections.defaultdict
是dict
的子类。
因此您可以只使用pd.DataFrame.from_dict
:
res = pd.DataFrame.from_dict(dd, orient='index')
res.columns = [f'SYN{i+1}' for i in res]
print(res)
SYN1 SYN2 SYN3
row1 Affinity None None
row2 Ahmc Garfield Medical Center
row3 Alamance Macbeth None
row4 None None None
row5 Mayday None None
答案 1 :(得分:4)
是的,您可以使用Series
df=pd.Series(d).apply(pd.Series).fillna('')
Out[55]:
0 1 2
row1 Affinity
row2 Ahmc Garfield Medical Center
row3 Alamance Macbeth
row4
row5 Mayday
或者通过数据框构造函数
df=pd.DataFrame(data=list(d.values()),index=d.keys())
Out[64]:
0 1 2
row1 Affinity None None
row2 Ahmc Garfield Medical Center
row3 Alamance Macbeth None
row4 None None None
row5 Mayday None None
然后我们创建列
df.columns='SYN'+(df.columns+1).astype(str)
df
Out[67]:
SYN1 SYN2 SYN3
row1 Affinity None None
row2 Ahmc Garfield Medical Center
row3 Alamance Macbeth None
row4 None None None
row5 Mayday None None