Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
491 views
in Technique[技术] by (71.8m points)

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

举个例子

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
# 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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...