Home  >  Q&A  >  body text

python - 相当于分组数据的合并,两个列表生成dataframe,但长度不同

举个例子

ntest=['a','b']
ltest=[[1,2],[4,5,6]]

最后我想得到下面这种结果:
a 1
a 2
b 4
b 5
b 6
这种该怎么做呢?

怪我咯怪我咯2741 days ago542

reply all(2)I'll reply

  • 天蓬老师

    天蓬老师2017-04-18 10:34:30

    # coding: utf-8
    
    import pandas as pd
    
    ntest = ['a','b']
    ltest = [[1,2], [4,5,6]]
    
    data = [(k, v) for k, l in zip(ntest, ltest) for v in l]
    
    print pd.DataFrame(data)

    reply
    0
  • 阿神

    阿神2017-04-18 10:34:30

    If the structure of the second array is determined, you can first split the second array into a one-dimensional array, and then traverse the two arrays to generate a dataframe.

    reply
    0
  • Cancelreply