python - 相当于分组数据的合并,两个列表生成dataframe,但长度不同
怪我咯
怪我咯 2017-04-18 10:32:30
0
2
535

举个例子

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

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

怪我咯
怪我咯

走同样的路,发现不同的人生

reply all(2)
Peter_Zhu
# 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)
阿神

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.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!