简体   繁体   中英

Map, Filter and Reduce procedures in Python

I am working through understanding the concepts of map, filter and reduce in Python. I am working in Spyder IDE with Python v3.6. 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. 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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