mercredi 28 janvier 2015

Python: using lambda function on Pandas Series, if.. else

I am trying to apply a filter on a series of values stored in a pandas series object. The desired output is the value itself if it meets the criterion otherwise zero. I can only get it to half work:



criterion = testdata.map(lambda x: x < 30 or x > 60)
testdata[criterion] =
Date
2015-01-05 62.358615
2015-01-06 64.349507
2015-01-13 61.726110
2015-01-14 63.574864
2015-01-15 66.020421
2015-01-16 63.041819
2015-01-20 61.458298
Name: rsi, dtype: float64


where:



testdata =
Date
2014-12-29 45.821073
2014-12-30 48.946830
2014-12-31 57.737593
2015-01-02 55.424047
2015-01-05 62.358615
2015-01-06 64.349507
2015-01-07 59.452935
2015-01-08 51.182115
2015-01-09 55.044419
2015-01-12 59.365017
2015-01-13 61.726110
2015-01-14 63.574864
2015-01-15 66.020421
2015-01-16 63.041819
2015-01-20 61.458298
2015-01-21 54.432635
2015-01-22 46.985675
2015-01-23 49.740981
2015-01-26 45.102370
2015-01-27 49.800855
Name: rsi, dtype: float64


but the following gives me quite a different result. Clearly I am not understanding what is happening in the background to produce this result:



criterion2 = testdata.map(lambda x: x if (x < 30 or x > 60) else 0)
testdata[criterion2]


results in:



rsi
0.000000 NaN
0.000000 NaN
0.000000 NaN
0.000000 NaN
62.358615 NaN
64.349507 NaN
0.000000 NaN
0.000000 NaN
0.000000 NaN
0.000000 NaN
61.726110 NaN
63.574864 NaN
66.020421 NaN
63.041819 NaN
61.458298 NaN
0.000000 NaN
0.000000 NaN
0.000000 NaN
0.000000 NaN
0.000000 NaN
Name: rsi, dtype: float64


I am looking for same formatting as the first output except with zeros for where the conditions are not met. Please help.


Aucun commentaire:

Enregistrer un commentaire