简体   繁体   English

如何将具有包含嵌套列表的数组值的字典转换为 Pandas 数据帧?

[英]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)

current df output当前 df 输出

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.

相关问题 如何将具有不同嵌套列表和嵌套数组的凌乱 Python 字典转换为 Pandas 数据框? - How do I convert a messy Python dictionary with different nested lists and a nested array into a Pandas dataframe? 将包含多个嵌套列表的字典转换为 pandas dataframe - Convert dictionary containing multiple nested lists to a pandas dataframe 将包含列表作为值的字典转换为DataFrame - Convert a dictionary containing lists as values to a DataFrame 如何从包含嵌套字典的字典创建 Pandas 数据框? - How do I create a Pandas Dataframe from a dictionary containing a nested dictionary? 将列表的嵌套字典有效地转换为pandas数据框 - Convert nested dictionary of lists into pandas dataframe efficiently 将嵌套列表的字典转换为pandas DataFrame - Convert a dictionary of nested lists to a pandas DataFrame 将带有字典列表的嵌套字典转换为 Pandas DataFrame - Convert a nested dictionary with lists of dictionaries to a Pandas DataFrame 如何将带有列表的嵌套dict转换为pandas dataframe的值 - How to convert nested dict with lists as values to pandas dataframe 如何将包含列表的 pandas dataframe 上传到 Google BigQuery 数组和结构格式? - How do I upload a pandas dataframe containing lists to Google BigQuery array and structs format? 如何将 Pandas DataFrame 转换为三级嵌套字典? - How can I convert a Pandas DataFrame to a three level nested dictionary?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM