简体   繁体   English

如何在pandas DataFrame中将列表元素从多列转置为行?

[英]How to transpose list elements from multiple columns into rows in pandas DataFrame?

I've got a dataframe like this: 我有一个这样的数据框:

                     args  inputs
0  [a.pl, foo, -bar, baz]  [a, b]
1  [a.pl, BAH, -bar, baz]  [a, c]

There are multiple columns, and all cells contain a list of strings. 有多列,并且所有单元格都包含字符串列表。 How can I transpose these lists to produce this: 我如何转置这些列表以产生此结果:

         args inputs
index i             
0     1  a.pl      a
      2   foo      b
      3  -bar    NaN
      4   baz    NaN
1     1  a.pl      a
      2   BAH      c
      3  -bar    NaN
      4   baz    NaN

Something like stack + unstack stack + unstack

df.stack().apply(pd.Series).stack().unstack(1)
Out[27]: 
     args inputs
0 0  a.pl      a
  1   foo      b
  2  -bar   None
  3   baz   None
1 0  a.pl      a
  1   BAH      c
  2  -bar   None
  3   baz   None

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

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