包含 pandas 数据框的数组和矩阵的字典列表

问题描述:

我有一个字典列表a看起来像这样:

I have a list of dictionaries a that looks like this:

a = {}
a[0]={}
a[0]['first_variable']=np.array([1,2,3,4,5])
a[0]['second_variable']=np.array([[1,2],[3,4],[5,6],[7,8],[9,10]])
a[1]={}
a[1]['first_variable']=np.array([1,2,3,4,5])
a[1]['second_variable']=np.array([[1,2],[3,4],[5,6],[7,8],[9,10]])

您会看到一些键包含一个数组,另一些包含一个矩阵...

As you can see some keys contain an array, other a matrix...

鉴于此字典,我想创建一个看起来像这样的数据框

Given this dictionary, I would like to create a dataframe that looks like this

a_dataframe = pd.DataFrame(columns=['dictionary','first_variable','second_variable_col1','second_variable_col2'])
a_dataframe['dictionary'] = np.array([1,1,1,1,1,2,2,2,2,2])
a_dataframe['first_variable']=np.array([1,2,3,4,5,1,2,3,4,5])
a_dataframe['second_variable_col1']=np.array([1,3,5,7,9,1,3,5,7,9])
a_dataframe['second_variable_col2']=np.array([2,4,6,8,10,2,4,6,8,10])

这应该以自动方式完成...即从字典键中获取名称,如果是矩阵,则添加col1,col2等... 我还应该在pandas数据框中引入一个column(可能在第一个位置),它告诉我原始字典的索引.在这种情况下,该列称为dictionary

This should be done in an automatic fashion...ie taking the names from the dictionary keys and in case of a matrix add col1, col2, etc... I should also introduce a column (possibly in the first position) in the pandas dataframe that tells me the index of the original dictionary.. in this case the column is called dictionary

你能帮我吗? 谢谢

dfs = []
for c, d in a.items():
    #iterate the outer dict and reconstruct the records to handle array and matrix
    temp_dict = ({'{}_col{}'.format(k,i):e for k,v in d.items() 
                      for i,e in enumerate(np.asarray(v).T.reshape(-1,5))})
    #append the dict indicator
    temp_dict['dictionary'] = c+1
    #append the df to the df list         
    dfs.append(pd.DataFrame(temp_dict))
df = pd.concat(dfs,axis=0,ignore_index=True)
print(df)

   dictionary  first_variable_col0  second_variable_col0  second_variable_col1
0           1                    1                     1                     2
1           1                    2                     3                     4
2           1                    3                     5                     6
3           1                    4                     7                     8
4           1                    5                     9                    10
5           2                    1                     1                     2
6           2                    2                     3                     4
7           2                    3                     5                     6
8           2                    4                     7                     8
9           2                    5                     9                    10