如何“取消堆叠”数据框列中的所有列表并成对组合结果?

时间:2018-06-13 06:40:12

标签: python list pandas dataframe

我有一个包含5列的数据框,每列包含可变长度的列表。这就是我的数据框中的一行:

               A                                         B
1 [aircrafts, they, agreement, airplane]  [are, built, made, built] 

现在我想'解包'或'取消堆叠'这些列表,以便每个单元格只包含一个值(一个单词)。在解包过程中,一列中单元格中的单词应与下一列中的相应值成对组合。结果将是:

        A            B
   1  aircrafts      are 
   2  they           built
   3  agreement      made
   4  airplane       built

作为参考,我的完整数据框如下所示:

   obj                       rel1  \
0                                 [Boeing]                    [sells]   
1   [aircrafts, they, agreement, airplane]  [are, built, made, built]   
2                       [exception, these]                 [are, are]   
3                        [sales, contract]       [regulated, consist]   
4                               [contract]               [stipulates]   
5                            [acquisition]                      [has]   
6                               [contract]                  [managed]   
7                               [employee]                      [act]   
8                    [salesperson, Boeing]          [change, ensures]   
9                     [airlines, airlines]            [related, have]   
10                                [Boeing]                     [keep]   

                  dep1                    rel2  \
0          [aircrafts]                    [to]   
1                   []              [on, with]   
2                   []           [of, of, for]   
3                   []  [by, of, with, of, of]   
4           [elements]            [across, as]   
5            [details]       [of, as, of, for]   
6                   []                    [by]   
7                   []               [as, for]   
8                   []  [Given, of, over, for]   
9            [company]               [to, for]   
10  [track, aircrafts]   [of, between, to, of]   

                                                 dep2  
0                                         [companies]  
1                                  [demand, customer]  
2                            [airplanes, scope, case]  
3   [means, contracts, companies, acquisitions, ai...  
4                          [acquisitions, conditions]  
5                [airplane, model, airplane, options]  
6                                       [salesperson]  
7                            [salesperson, contracts]  
8                     [term, contracts, time, client]  
9                                    [other, example]  
10        [relationships, companies, airlines, buyer]        

如何在python中执行'解包'和重新排列操作?如果可以在数据帧本身上执行这些操作,那将是很好的。如果这被证明是困难或不可能的,有没有办法在将它们组合到数据帧之前重新排列列表中的数据?

非常感谢您提供任何帮助或建议。

1 个答案:

答案 0 :(得分:1)

我相信你希望能够将这些行读成句子

\usepackage{tocloft}
\addto\captionsenglish{\renewcommand{\contentsname}{Table of Contents}}
\renewcommand{\cfttoctitlefont}{\hfill\normalsize}
\renewcommand{\cftaftertoctitle}{\hfill}