我有一个像这样的列表字典
d = {'key1':['banana','apple','mango'],
'key2':['banana','orange'],
'key3':['apple','melon','orange','mango']}
现在,我想从中创建数据框。数据框必须看起来像这样
'banana' | 'apple' | 'mango' | NaN
'banana' | 'orange' | NaN | NaN
'apple' | 'melon' | 'orange'|'mango'
答案 0 :(得分:0)
使用
d = {'key1':['banana','apple','mango'],
'key2':['banana','orange'],
'key3':['apple','melon','orange','mango']}
>>> pd.DataFrame(list(d.values()))
0 1 2 3
0 banana apple mango None
1 banana orange None None
2 apple melon orange mango
答案 1 :(得分:0)
您要使用orient='index'
查找from_dict
:
pd.DataFrame.from_dict(d, orient='index')
0 1 2 3
key1 banana apple mango None
key2 banana orange None None
key3 apple melon orange mango
如果要数字索引:
pd.DataFrame.from_dict(d, orient='index').reset_index(drop=True)
0 1 2 3
0 banana apple mango None
1 banana orange None None
2 apple melon orange mango