2 回答
TA贡献1995条经验 获得超2个赞
无需循环。您可以使用 pandas 方法:
dp['x_lin'] = x[np.abs(np.subtract.outer(x,dp.xlist.values)).argmin(0)]
dp['y_lin'] = 0
dp.y_lin[np.abs(dp.x_lin-dp.xlist)<0.001] = dp.ylist
您也可以将第一行替换为等效行:
dp['x_lin'] = x[np.abs(x[None,:]-dp.xlist[:,None]).argmin(1)]
输出:
xlist ylist x_lin y_lin
0 0.017108 0.902494 0.020202 0.000000
1 0.019659 0.741981 0.020202 0.741981
2 0.030310 0.920884 0.030303 0.920884
3 0.032064 0.255826 0.030303 0.000000
4 0.046168 0.562761 0.050505 0.000000
5 0.060758 0.583044 0.060606 0.583044
6 0.118274 1.000000 0.121212 0.000000
7 0.125385 1.000000 0.121212 0.000000
8 0.140418 1.000000 0.141414 1.000000
9 0.153699 1.000000 0.151515 0.000000
10 0.186998 0.293743 0.191919 0.000000
11 0.215375 0.112288 0.212121 0.000000
12 0.217875 0.260883 0.222222 0.000000
13 0.250591 0.325953 0.252525 0.000000
14 0.262788 0.084916 0.262626 0.084916
15 0.287382 0.104910 0.282828 0.000000
16 0.325902 0.088418 0.323232 0.000000
17 0.377885 0.312025 0.373737 0.000000
18 0.473086 0.068632 0.474747 0.000000
19 0.485449 0.059624 0.484848 0.059624
20 0.557334 0.734376 0.555556 0.000000
21 0.572133 0.709392 0.575758 0.000000
22 0.610553 0.840687 0.606061 0.000000
23 0.626902 0.737930 0.626263 0.737930
24 0.630276 0.084787 0.626263 0.000000
25 0.637779 0.091535 0.636364 0.000000
26 0.717553 0.074411 0.717172 0.074411
27 0.742187 0.092770 0.737374 0.000000
28 0.757873 0.532881 0.757576 0.532881
29 0.780933 0.528202 0.777778 0.000000
30 0.836708 0.422615 0.838384 0.000000
31 0.920814 0.359896 0.919192 0.000000
32 0.938639 0.355241 0.939394 0.355241
33 0.954564 0.280989 0.959596 0.000000
34 0.978567 0.649749 0.979798 0.000000
35 0.995067 0.769272 1.000000 0.000000
输出图:
TA贡献1810条经验 获得超4个赞
这看起来不错。
是的,您正在覆盖每个 .但是您还为每个 xa 添加了 tmp。我不太清楚你的x是什么。但是尝试:i
fill = []
for i in dp.index:
tmp = 0
for xa in x:
if abs(dp.xlist[i]-xa)<0.001:
tmp = dp.ylist[i]
fill.append(tmp)
- 2 回答
- 0 关注
- 113 浏览
添加回答
举报