简体   繁体   English

将多索引数据框堆叠为字典的数据框

[英]Unstacking multi-indexed dataframe into dataframe of dictionaries

So I multiplied a dataframe of dictionaries, by another dataframe of factors. 因此,我将字典的数据框乘以因子的另一个数据框。 I want to know how to get the resulting stacked dataframe from that multiplication back into a dataframe of dictionaries. 我想知道如何将乘法运算得到的堆叠数据帧返回到字典数据帧中。

Say given df, and df2: 说给定df和df2:

df = pd.DataFrame({'A': [{"ab":1, "b":2, "c":3}, {'b':4, 'c':5, 'ab':6}], 
               'B': [{"ab":7, "b":8, "c":9}, {'b':10, 'c':11, 'ab':12}]})

                             A                             B
0     {'b': 2, 'c': 3, 'ab': 1}     {'b': 8, 'c': 9, 'ab': 7}
1     {'b': 4, 'c': 5, 'ab': 6}  {'b': 10, 'c': 11, 'ab': 12}

df2 = pd.DataFrame({'A': [2, 3], 
               'B': [3, 4]})

   A  B
0  2  3
1  3  4

Using this to help multiply them together 来帮助他们一起繁殖

In[11]: df.stack().apply(pd.Series)
Out[11]: 
     ab   b   c
0 A   1   2   3
  B   7   8   9
1 A   6   4   5
  B  12  10  11

Then applied a similar function to the df2 to return the dataframe as a 1xN series 然后将类似的功能应用于df2,以1xN系列的形式返回数据帧

In[12]: ser = pd.Series(df2.stack().apply(pd.Series).reset_index().iloc[:, -1])
In[13]: ser
Out[13]: 
0    2
1    3
2    3
3    4

Then used the function from the link to multiply a dataframe and a series 然后使用链接中的函数将一个数据框和一个序列相乘

In[14]: func = lambda x: np.asarray(x) * np.asarray(ser)
In[15]: df.stack().apply(pd.Series).apply(func)
Out[15]: 
     ab   b   c
0 A   2   4   6
  B  21  24  27
1 A  18  12  15
  B  48  40  44

How do I 'unstack' the above dataframe back into the same format as df? 如何将上述数据帧“解栈”回与df相同的格式?

                                A                                B
0        {'b': 4, 'c': 6, 'ab': 2}     {'b': 24, 'c': 27, 'ab': 21}
1     {'b': 12, 'c': 15, 'ab': 18}     {'b': 40, 'c': 44, 'ab': 48}

Transformed the data into a dictionary. 将数据转换成字典。

In[1]: df.to_dict('r')
Out[2]: [{'ab': 2, 'b': 4, 'c': 6},
 {'ab': 21, 'b': 24, 'c': 27},
 {'ab': 18, 'b': 12, 'c': 15},
 {'ab': 24, 'b': 20, 'c': 22},
 {'ab': 48, 'b': 40, 'c': 44},
 {'ab': 48, 'b': 40, 'c': 44}]

Then zipped all the level values with their corresponding dict, which was appended to a list 然后将所有级别值及其对应的字典压缩,将其追加到列表中

list = []
for x in zip(df.index.get_level_values(0),df.index.get_level_values(1),   df.to_dict('r')):
    list.append(x)
new = pd.DataFrame(list)
new = new.pivot(index=0, columns=1, values=2)

Then reset the multi-index and got rid of the new column 然后重置多索引并摆脱新列

new.reset_index().ix[:, 1:]

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

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