以下是我的表格:
df=pd.DataFrame(np.random.rand(60).reshape(20,3))
df.columns=['price','time','1-D']
现在,我想再添加9个列,这些列是' 2-D',' 3-D',....,' 10-D&# 39 ;. ' 2-D'中的值是:' NaN'加上' 1-D'中的所有值,但删除最后一个值(因为我首先添加' NaN'。与' 3-D'相同:& #39; NaN' +列[' 2-D']。
以下是我想得到的结果:
Out[153]:
price time 1-D 2-D 3-D 4-D 5-D \
0 0.414901 0.173058 0.403901 NaN NaN NaN NaN
1 0.918011 0.262294 0.641516 0.403901 NaN NaN NaN
2 0.524934 0.051661 0.329890 0.641516 0.403901 NaN NaN
3 0.654412 0.867551 0.263398 0.329890 0.641516 0.403901 NaN
4 0.107087 0.192514 0.598509 0.263398 0.329890 0.641516 0.403901
5 0.232152 0.144453 0.107699 0.598509 0.263398 0.329890 0.641516
6 0.105883 0.243597 0.602938 0.107699 0.598509 0.263398 0.329890
7 0.269133 0.036553 0.749552 0.602938 0.107699 0.598509 0.263398
8 0.144464 0.516613 0.179333 0.749552 0.602938 0.107699 0.598509
9 0.901749 0.908061 0.025336 0.179333 0.749552 0.602938 0.107699
10 0.247895 0.231501 0.157868 0.025336 0.179333 0.749552 0.602938
11 0.132763 0.501174 0.171977 0.157868 0.025336 0.179333 0.749552
12 0.165358 0.407833 0.284734 0.171977 0.157868 0.025336 0.179333
13 0.960481 0.511533 0.249719 0.284734 0.171977 0.157868 0.025336
14 0.016045 0.512287 0.206942 0.249719 0.284734 0.171977 0.157868
15 0.948984 0.911282 0.269408 0.206942 0.249719 0.284734 0.171977
16 0.470319 0.468252 0.289175 0.269408 0.206942 0.249719 0.284734
17 0.751353 0.167094 0.531566 0.289175 0.269408 0.206942 0.249719
18 0.854261 0.894898 0.513595 0.531566 0.289175 0.269408 0.206942
19 0.016631 0.110026 0.797857 0.513595 0.531566 0.289175 0.269408
6-D 7-D 8-D 9-D 10-D
0 NaN NaN NaN NaN NaN
1 NaN NaN NaN NaN NaN
2 NaN NaN NaN NaN NaN
3 NaN NaN NaN NaN NaN
4 NaN NaN NaN NaN NaN
5 0.403901 NaN NaN NaN NaN
6 0.641516 0.403901 NaN NaN NaN
7 0.329890 0.641516 0.403901 NaN NaN
8 0.263398 0.329890 0.641516 0.403901 NaN
9 0.598509 0.263398 0.329890 0.641516 0.403901
10 0.107699 0.598509 0.263398 0.329890 0.641516
11 0.602938 0.107699 0.598509 0.263398 0.329890
12 0.749552 0.602938 0.107699 0.598509 0.263398
13 0.179333 0.749552 0.602938 0.107699 0.598509
14 0.025336 0.179333 0.749552 0.602938 0.107699
15 0.157868 0.025336 0.179333 0.749552 0.602938
16 0.171977 0.157868 0.025336 0.179333 0.749552
17 0.284734 0.171977 0.157868 0.025336 0.179333
18 0.249719 0.284734 0.171977 0.157868 0.025336
19 0.206942 0.249719 0.284734 0.171977 0.157868
答案 0 :(得分:2)
我认为你可以使用assign和shift:
df.assign(**{'{}-D'.format(i): df['1-D'].shift(i-1) for i in range(2,11)})
一点解释:
{'{}-D'.format(i): df['1-D'].shift(i-1) for i in range(2,11)}
构建一个dict,列名为键,列系列为值。 **运算符解包字典键和值,并将它们作为参数提供给assign函数。它与呼叫相同:
df.assign(2-D=xxx, 3-D=yyy...)