简体   繁体   English

使用Python映射,过滤和缩减过程

[英]Map, Filter and Reduce procedures in Python

I am working through understanding the concepts of map, filter and reduce in Python. 我正在通过理解Python中的map,filter和reduce的概念进行工作。 I am working in Spyder IDE with Python v3.6. 我正在使用Python v3.6在Spyder IDE中工作。 I have a data frame: 我有一个数据框:

Cap    OC_y       GMWB         PE        Acc
0.01    0.0065  0.560840708 0.646683673 0.515243902
0.0105  0.0068  0.586725664 0.676530612 0.53902439
0.011   0.0071  0.612610619 0.706377551 0.562804878
0.0115  0.0073  0.629867257 0.72627551  0.578658537
0.012   0.0076  0.655752212 0.756122449 0.602439024
0.0125  0.0079  0.681637168 0.785969388 0.626219512
0.013   0.0082  0.707522124 0.815816327 0.65
0.0135  0.0085  0.73340708  0.845663265 0.673780488
0.014   0.0087  0.750663717 0.865561224 0.689634146
0.0145  0.009   0.776548673 0.895408163 0.713414634
0.015   0.0093  0.802433628 0.925255102 0.737195122

I want to select Cap records in increments of .005. 我想选择以0.005为增量的上限记录。 Please see below: 请看下面:

Cap    OC_y        GMWB          PE         Acc
0.01    0.0065  0.560840708 0.646683673 0.515243902
0.015   0.0093  0.802433628 0.925255102 0.737195122

In this case, wouldn't a map function work in this case? 在这种情况下,地图功能无法正常工作吗?

map(lambda Cap: Cap + 0.05, data)

Any other option would be great. 任何其他选择都很好。 I ideally need it to work in a way where I can incrementally select the records based on a certain value. 理想情况下,我需要它以某种方式工作,以便我可以基于某个值逐步选择记录。

Assuming these are multiples of 0.005, you can divide and then use np.isclose to select. 假设这些是0.005的倍数,则可以除以然后使用np.isclose进行选择。

v = df.Cap / 0.005
out = df[np.isclose(v, v.astype(int))]

If this isn't the case, then I recommend subtracting the delta until it is, and then repeating the above: 如果不是这种情况,那么我建议减去增量,直到达到为止,然后重复以上操作:

v = (df.Cap - (df.Cap % 0.005).iat[0]) / 0.005
out = df[np.isclose(v, v.astype(int))]

print(out)

      Cap    OC_y      GMWB        PE       Acc
0   0.010  0.0065  0.560841  0.646684  0.515244
10  0.015  0.0093  0.802434  0.925255  0.737195

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM