I have a dataframe that looks like this:
>>> df = pd.DataFrame( {'InLevel_03': [12, 12, 13, 12, 11,], 'InLevel_02': [11.5, 11.5, 12.5, 11.5, 10.5], 'InLevel_01': [11, 10.5, 12, 10.5, 9], 'OutLevel_01': [10.5, 10, 11.5, 10, 8.5], 'OutLevel_02': [10, 9.5, 11, 9.5, 8], 'OutLevel_03': [9.5, 9, 10, 9, 7.5]} )
>>> df
InLevel_03 InLevel_02 InLevel_01 OutLevel_01 OutLevel_02 OutLevel_03
0 12 11.5 11.0 10.5 10.0 9.5
1 12 11.5 10.5 10.0 9.5 9.0
2 13 12.5 12.0 11.5 11.0 10.0
3 12 11.5 10.5 10.0 9.5 9.0
4 11 10.5 9.0 8.5 8.0 7.5
If the given value is 0.5, I want to check if there's a gap bigger than the given value in a row. For example, in the 2nd row, there's a gap between InLevel_02(11.5) and InLevel_01(10.5), which is 11. In the 5th row, the gaps are 10 and 9.5, between InLevel_02(10.5) and InLevel_01(9.0).
The result of this job would look like this:
gapLevel count # row number, column name of each gap
11 2 # (1, InLevel_02 - 1, InLevel_01), (3, InLevel_02 - 3, InLevel_01)
10.5 1 # (2, OutLevel_02 - 2, OutLevel_03)
10 1 # (4, InLevel_02 - 4, InLevel_01)
9.5 1 # (4, InLevel_02 - 4, InLevel_01)
I tried converting the dataframe into an array(using .to_records) and comparing each value with its next value using loops, but the code gets too complicated when there are more than 1 level between two values and I'd like to know if there are more efficient ways to do this.
gapLevelmatching with values in inputdf?gapLevelandcount. You don't want to merge these columns in orginial `df'(12, 11.5, 11.0, 10.5 , 10.0 , 9.5).If it's not by 0.5 but 1(1 gap exists) or 1.5(2 gaps exist), it's not continuous and there's a gap.