简体   繁体   English

从嵌套字典中的值构造数据框

[英]Construct dataframe from values in nested dictionary

I have a list of lists of dictionaries that I'm trying to convert to a pandas DataFrame but I'm unable to use pandas.DataFrame.from_dict() because I want the value of the 'name' key to be the column header and the value of the 'duration' key to be the row value. 我有一个字典列表,我正在尝试转换为pandas DataFrame,但我无法使用pandas.DataFrame.from_dict()因为我希望'name'键的值是列标题和'duration'键的值为行值。 Any suggestions on how I can make this work? 关于如何使这项工作的任何建议?

[[{'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'}]]

Desired Output: 期望的输出:

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

You can flatten your list of lists via itertools.chain , then pivot your dataframe. 您可以通过itertools.chain展平列表列表,然后pivot数据框。 The trick here is to use an index which counts cumulatively by your grouper. 这里的诀窍是使用一个由石斑鱼累积计数的指数。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM