我正在尝试从2个数据帧df3和df4的for循环中创建列表列表
df3 = pd.DataFrame(np.random.randn(100, 4), columns=list('ABCD'))
df4 = pd.DataFrame(np.random.randn(100, 2), columns=list('EF'))
l=[]
for i in range(len(df3)-2):
s=df3[i:i+2].values.tolist()
s.append(df4.iloc[i+2,1]) #this is what one element looks like and works
l.append(s)
我得到的输出是
[[[[[[[[[''],
[[[-1.6901730393462748,
0.9398299757220133,
0.5402878560205543,
-0.2355701586445652],
….. 2.300820924304919]]
我正在寻找的输出是
[[[[-1.6901730393462748,
0.9398299757220133,
0.5402878560205543,
-0.2355701586445652],
[1.789968968313937,
0.3792557248306339,
0.5701432920348918,
1.3781227005819763]],
1.087047297584572]],
[[[1.789968968313937,
0.3792557248306339,
0.5701432920348918,
1.3781227005819763],
and so on.]]]]
我找不到合适的线程,如果有,请指向正确的方向。
答案 0 :(得分:1)
我仅在此处修复您的代码,您的代码中有很多问题,例如,for循环应使用range
而不是int
,并且ix
将不再起作用我正在使用iloc
替换它,而list
应该使用append
l=[]
for i in range(len(df3)-2):
s=df3[i:i+2].values.tolist()
s.append(df4.iloc[i+2,1]) #this is what one element looks like and works
l.append(s) # this is intended to create a list of all the elements