我有一个像这样的嵌套列表
a=[ [[12,23],[31,41]], [[53,68],[77,87]] ]
我想像这样创建一个pandas数据框
id1 id2 c2 c3
1 1 12 31
1 2 53 77
2 1 23 41
2 2 68 87
我的想法是这样做:
ids = {'id1':[1,1,2,2],'id2':[1,2,1,2]}
df1 = pd.DataFrame(ids)
L = [[t[idx] for t in l] for idx in [0,1] for l in a]
df2 = pd.DataFrame(L, columns=['c1','c2'])
pd.concat([df1,df2], axis=1)
有没有办法在没有这么多命令的情况下实现相同的结果?
答案 0 :(得分:2)
from itertools import product
# generate id1 and id2 using `itertools.product`
v = np.arange(len(a))
i = pd.DataFrame(np.array(list(product(v, v))) + 1)
# concatenate `a` along the 0th axis
j = pd.DataFrame(np.concatenate(a, axis=0))
# concatenate `i` and `j`
df = pd.concat([i, j], axis=1)
# set the column names
df.columns = ['id1', 'id2', 'c2', 'c3']
id1 id2 c2 c3
0 1 1 12 23
1 1 2 31 41
2 2 1 53 68
3 2 2 77 87