0

my input:

first=pd.Series([0,1680,5000,14999,17000])
last =pd.Series([4999,7501,10000,16777,21387])
dd=pd.concat([first, last], axis=1)

I trying find&compare second value in first column (e.g. 1680) and "range" previous row between first value in first column to first value in second column(e.g. from 0 to 4999). So in my condition value 1680 fall in range previous row between 0 to 4999, also 3td value in first column 5000 fall in range previous row between 1680 to 7501, but other values (e.g. 14999, 17000) not in range of previous rows.
My expect output something like this:
[1680], [5000] so show only values that fall in my condition
I trying with diff(): dd[0].diff().gt(dd[1]) or reshape/shift but not really success

2
  • Can you please put the expected dataframe? Commented Aug 29, 2021 at 11:40
  • I am not really necessary output as df, only value that fall in my condition, but if solution as df it will be great too Commented Aug 29, 2021 at 11:52

1 Answer 1

1

Use shift and between to compare a row with the previous one:

>>> df[0].loc[df[0].between(df[0].shift(), df[1].shift())]
1    1680
2    5000
Name: 0, dtype: int64

Details of shift:

>>> pd.concat([df[0], df.shift()], axis=1)
       0        0        1
0      0      NaN      NaN
1   1680      0.0   4999.0
2   5000   1680.0   7501.0
3  14999   5000.0  10000.0
4  17000  14999.0  16777.0
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.