简体   繁体   English

如何从包含数组中嵌套值列表、字典中的输出构建熊猫数据框

[英]How to construct a pandas dataframe from an output consisting of a nested list of values within an array, within a dictionary

The data outputs from a class variable as such:类变量的数据输出如下:

{'coupling.2.b': array([[0.00247262],
    [0.00248247],
    [0.00249233],
    ...,
    [0.01106487],
    [0.01106487],
    [0.01106487]]), 'coupling.2.ca': array([[9.98543405e-09],
    [9.97095234e-09],
    [9.95652127e-09],
    ...,
    [1.22356842e-08],
    [1.22356843e-08],
    [1.22356843e-08]]), 'coupling.2.gnmda': array([[0.002     ],
    [0.002     ],
    [0.002     ],
    ...,
    [0.00200253],
    [0.00200253],
    [0.00200253]]), 'coupling.2.nu': array([[1.30000000e-05],
    [1.30000000e-05],
    [1.30000000e-05],
    ...,
    [1.29998715e-05],
    [1.29998715e-05],
    [1.29998715e-05]]), 'coupling.2.nutilde': array([[1.30000000e-05],
    [1.30000000e-05],
    [1.30000000e-05],
    ...,
    [1.29971456e-05],
    [1.29971456e-05],
    [1.29971456e-05]]), 'pop.2.q': array([[11.07967784],
    [11.07967784],
    [11.07967785],
    ...,

I'm trying to convert this to a pandas df, with each dict key containing the column title and each respective dict value as its data series.我正在尝试将其转换为 Pandas df,每个 dict 键都包含列标题和每个相应的 dict 值作为其数据系列。 Thanks for the help.谢谢您的帮助。

Is this what you're looking for?这是你要找的吗?

from numpy import array

data = {
    "coupling.2.b": array([[0.00247262], [0.00248247], [0.00249233], [0.01106487], [0.01106487], [0.01106487]]),
    "coupling.2.ca": array([[9.98543405e-09], [9.97095234e-09], [9.95652127e-09], [1.22356842e-08], [1.22356843e-08], [1.22356843e-08]]),
    "coupling.2.gnmda": array([[0.002], [0.002], [0.002], [0.00200253], [0.00200253], [0.00200253]]),
    "coupling.2.nu": array([[1.30000000e-05], [1.30000000e-05], [1.30000000e-05], [1.29998715e-05], [1.29998715e-05], [1.29998715e-05]]),
    "coupling.2.nutilde": array([[1.30000000e-05], [1.30000000e-05], [1.30000000e-05], [1.29971456e-05], [1.29971456e-05], [1.29971456e-05]]),
}

data = {k: v.flatten() for k, v in data.items()}
df = pd.DataFrame(data)

print(df)
   coupling.2.b  coupling.2.ca  coupling.2.gnmda  coupling.2.nu  coupling.2.nutilde
0      0.002473   9.985434e-09          0.002000       0.000013            0.000013
1      0.002482   9.970952e-09          0.002000       0.000013            0.000013
2      0.002492   9.956521e-09          0.002000       0.000013            0.000013
3      0.011065   1.223568e-08          0.002003       0.000013            0.000013
4      0.011065   1.223568e-08          0.002003       0.000013            0.000013
5      0.011065   1.223568e-08          0.002003       0.000013            0.000013

The trick here was to flatten all of the arrays via the numpy.ndarray.flatten method.这里的技巧是通过numpy.ndarray.flatten方法展平所有数组。 Once you do that, it'll pass into a dataframe seamlessly.一旦你这样做了,它就会无缝地传递到一个数据帧中。

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

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