Quantcast
Viewing all articles
Browse latest Browse all 14215

Identifying row numbers where value is stable before and after the value in the column hits a specified value

EDITED

I have a pandas dataframe like so:

data = {'ID': ['A', 'A',    'A',    'A',    'A',    'A',    'A',    'A',    'A',    'A',    'A',    'A',    'A',    'A',    'A',    'A',    'A',    'A',    'A',    'A',    'A',    'A',    'A',    'A',    'A',    'A',    'A',    'A',    'A',    'A',    'A',    'A',    'A',    'A',    'A',    'A',    'A',    'A',    'A',    'A',    'A',    'A',    'B',    'B',    'B',    'B',    'B',    'B',    'B',    'B',    'B',    'B'],'column_1': [0, 0,  0,  0,  0.1,    1,  1.5,    2,  3,  4,  4.5,    5,  4.9,    3,  2,  1.8,    1,  0,  0,  1,  3,  0,  1.3,    2,  3,  4.3,    4.8,    5,  4.2,    3.5,    3,  2.6,    2,  1.9,    1,  0,  0,  0,  0,  0,  0.1,    0.2,    0.3,    1,  2,  3,  5,  4,  2,  0.5,    0,  0],'column_2': [13,    25, 96, 59, 5,  92, 82, 141,    50, 85, 84, 113,    119,    128,    8,  133,    82, 10, 15, 62, 11, 68, 18, 24, 37, 55, 83, 48, 13, 81, 43, 36, 56, 43, 36, 46, 45, 127,    55, 67, 113,    98, 78, 78, 57, 131,    121,    126,    142,    51, 64, 95]}    ID  column_1  column_20   A       0.0        131   A       0.0        252   A       0.0        963   A       0.0        594   A       0.1         55   A       1.0        926   A       1.5        827   A       2.0       1418   A       3.0        509   A       4.0        8510  A       4.5        8411  A       5.0       11312  A       4.9       11913  A       3.0       12814  A       2.0         815  A       1.8       13316  A       1.0        8217  A       0.0        1018  A       0.0        1519  A       1.0        6220  A       3.0        1121  A       0.0        6822  A       1.3        1823  A       2.0        2424  A       3.0        3725  A       4.3        5526  A       4.8        8327  A       5.0        4828  A       4.2        1329  A       3.5        8130  A       3.0        4331  A       2.6        3632  A       2.0        5633  A       1.9        4334  A       1.0        3635  A       0.0        4636  A       0.0        4537  A       0.0       12738  A       0.0        5539  A       0.0        6740  A       0.1       11341  A       0.2        9842  B       0.3        7843  B       1.0        7844  B       2.0        5745  B       3.0       13146  B       5.0       12147  B       4.0       12648  B       2.0       14249  B       0.5        5150  B       0.0        6451  B       0.0        95

Tracing back from when the value hits 5 in column_1, I want to find the value in column_2 just before the value in column_1 increased from 0 and just after it came back down to 0. So, in the data frame above, the values in column_2 would be 5, 10 and 18, 46.I want to perform some arithmetic and would like to add 2 columns before& after with those values grouped by the ID column.The expected output would be:

    ID  column_1  column_2  Before  After0   A       0.0        13       0      01   A       0.0        25       0      02   A       0.0        96       0      03   A       0.0        59       0      04   A       0.1         5       0      05   A       1.0        92       0      06   A       1.5        82       0      07   A       2.0       141       0      08   A       3.0        50       0      09   A       4.0        85       0      010  A       4.5        84       0      011  A       5.0       113       5     1012  A       4.9       119       0      013  A       3.0       128       0      014  A       2.0         8       0      015  A       1.8       133       0      016  A       1.0        82       0      017  A       0.0        10       0      018  A       0.0        15       0      019  A       1.0        62       0      020  A       3.0        11       0      021  A       0.0        68       0      022  A       1.3        18       0      023  A       2.0        24       0      024  A       3.0        37       0      025  A       4.3        55       0      026  A       4.8        83       0      027  A       5.0        48      18     4628  A       4.2        13       0      029  A       3.5        81       0      030  A       3.0        43       0      031  A       2.6        36       0      032  A       2.0        56       0      033  A       1.9        43       0      034  A       1.0        36       0      035  A       0.0        46       0      036  A       0.0        45       0      037  A       0.0       127       0      038  A       0.0        55       0      039  A       0.0        67       0      040  A       0.1       113       0      041  A       0.2        98       0      042  B       0.3        78       0      043  B       1.0        78       0      044  B       2.0        57       0      045  B       3.0       131       0      046  B       5.0       121      78     6447  B       4.0       126       0      048  B       2.0       142       0      049  B       0.5        51       0      050  B       0.0        64       0      051  B       0.0        95       0      0

For a given ID if column_1 starts with a non zero value, it should give the first value of column_2 for that group.

The rest of the rows in Before and After can be filled with null or zeroes.Is there an elegant way to achieve this?


Viewing all articles
Browse latest Browse all 14215

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>