l1 = dfx.v1.unique()
l2 = dfx.v2.unique()
day_parts = [1,2,3,4]
week_days = list(calendar.day_name)
df = pd.DataFrame() # what goes here?
l1[0] | l1[1] | ... | l2[0] | l2[1] | ... | day_parts[0] | day_parts[1] | .....
--------------------------------------------------------------------------------
v a c x s ... 2 1 .....
v a c x s ... 2 1 .....
v a c x s ... 2 1 .....
v a c x s ... 2 1 .....
v a c x s ... 2 1 .....
从多个列名列表构建数据框的最佳方法是什么?当然可以附加所有列表,但我想知道是否有更好的pythonic-pandas方法来实现它?
答案 0 :(得分:0)
你需要:
L = [l1, l2, day_parts, week_days]
import itertools
cols = list(itertools.chain(*L))
cols = [item for sublist in L for item in sublist]
cols = sum(L, [])
cols = l1 + l2 + day_parts + week_days
df = pd.DataFrame(columns=cols)