[英]How do I convert a dictionary with array values containing nested lists into pandas dataframe?
I have a dictionary containing an array of nested lists, where each dictionary key references a unique array.我有一个包含嵌套列表数组的字典,其中每个字典键引用一个唯一的数组。 It outputs like this:
它输出如下:
{'pop.2.q': array([[11.07967784],
[11.07967784],
[11.07967785],
...,
[11.16404993],
[11.16404993],
[11.16404993]]), 'pop.2.v': array([[0.00011533],
[0.00011533],
[0.00011533],
...,
[0.00014513],
[0.00014513],
[0.00014513]]), 'propagator.1.phi': array([[0.],
[0.],
[0.],
...,
[0.],
[0.],
I'm trying to convert it into a pandas dataframe, where each dictionary key (ie. 'pop.2.q', 'pop.2.v', etc) is a column.我正在尝试将其转换为 Pandas 数据框,其中每个字典键(即“pop.2.q”、“pop.2.v”等)是一列。 Right now, my dataframe looks like this with the following code:
现在,我的数据框如下所示:
df = pd.DataFrame(data=[Res.data], index=Res.time)
Any help with how to unpack this and properly populate the df would be super appreciated - thanks!任何有关如何解包并正确填充 df 的帮助将不胜感激 - 谢谢!
Assuming all values in dictionary are lists with same number of (flattened) items, this should do the trick:假设字典中的所有值都是具有相同数量(扁平)项目的列表,这应该可以解决问题:
import numpy as np
import pandas as pd
df = pd.DataFrame(dict([(x,np.array(list(y)).flatten()) for x,y in d.items()]))
assuming d is your dictionary.假设 d 是你的字典。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.