且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将列表列表堆叠到pandas数据框中

更新时间:2023-02-10 13:07:34

使用 split 首先是由正则表达式表示的list-,\s+是带有一个或多个空格的逗号,然后是 numpy.concatenate 和最后一个DataFrame构造函数:

Use split for lists first by regex - ,\s+ for comma with one or more spaces, and then numpy.repeat with flatenning by numpy.concatenate and last DataFrame constructor:

a = x.a.str.split(",\s+")
b = np.repeat(x.b.values, a.str.len())
c = np.concatenate(a.values)

df = pd.DataFrame({'letter':c, 'num':b})
print (df)
  letter  num
0      x    1
1      y    1
2      x    0
3      t    0
4      x    0
5      r    0
6      y    1
7      t    1