Python 从嵌套字典中的值构造dataframe

Python 从嵌套字典中的值构造dataframe,python,pandas,dictionary,Python,Pandas,Dictionary,我有一个字典列表,我正试图将其转换为pandas数据帧,但我无法使用pandas.DataFrame.from_dict(),因为我希望“name”键的值为列标题,“duration”键的值为行值。有什么建议可以让我这样做吗 [[{'duration': 21.82, 'name': 'ABC'}, {'duration': 3.9, 'name': 'DEF'}, {'duration': 105.78, 'name': 'GHI'}, {'duration': 63.14, 'name':

我有一个字典列表,我正试图将其转换为pandas数据帧,但我无法使用
pandas.DataFrame.from_dict()
,因为我希望“name”键的值为列标题,“duration”键的值为行值。有什么建议可以让我这样做吗

[[{'duration': 21.82, 'name': 'ABC'},
{'duration': 3.9, 'name': 'DEF'},
{'duration': 105.78, 'name': 'GHI'},
{'duration': 63.14, 'name': 'JKL'}],
[{'duration': 18.9, 'name': 'ABC'},
{'duration': 56.01, 'name': 'DEF'},
{'duration': 38.36, 'name': 'GHI'},
{'duration': 34.16, 'name': 'JKL'}]]
期望输出:

    ABC    DEF    GHI     JKL
0  21.82   3.9   105.78   63.14
1  18.9   56.01  38.36    34.16

您可以通过,然后通过数据帧展平列表列表。这里的诀窍是使用一个指数,该指数按你的石斑鱼累计计数

from itertools import chain

df = pd.DataFrame(list(chain.from_iterable(L)))

res = df.pivot(index=df.groupby('name').cumcount(), columns='name')
res.columns = res.columns.droplevel(0)  # remove unwanted column level

print(res)

name    ABC    DEF     GHI    JKL
0     21.82   3.90  105.78  63.14
1     18.90  56.01   38.36  34.16