如何从2列包含数字序列中提取数字范围,并打印两列的范围(不同的增量值)? [英] How to extract numeric ranges from 2 columns containig numeric sequences and print the range from both columns (different increment values)?
问题描述
列1:(col1数字递减和/或递减= 1)
1
2
3
5
7
8
9
Col 2:(Col2数字增量和/或减量= 4) p>
113
109
105
90
94
98
102
需要从两列中提取数字范围,并根据序列间隔发生打印在这两列中的任何一列,结果应如下:
1,3,105,113
5,5,90 ,90
7,9,94,102
已经收到了一个非常有用的方法来使用python的熊猫库由@MaxU生成数字范围,基于两列上检测到的断点,使用col1和col2 =的标准增加和/或减少es by 1。
在这种情况下,独特的区别在于,对于这两个列,应用的增量/减量条件对于它们中的每一个都是不同的。
尝试这样:
在[42]中:df
出[42]:
Col1 Col2
0 1 113
1 2 109
2 3 105
3 5 90
4 7 94
5 8 98
6 9 102
在[43]中:df.groupby(df.diff()。abs()。ne ([1,4])。any(1).cumsum())。agg(['min','max'])
输出[43]:
Col1 Col2
min最大最小
1 1 3 105 113
2 5 5 90 90
3 7 9 94 102
说明:我们的目标是将这些行与增量/减量 [1,4]
code> Col1 , Col2
相应地:
在[44]中:df.diff()。abs()
出[44]:
Col1 Col2
0 NaN NaN
1 1.0 4.0
2 1.0 4.0
3 2.0 15.0
4 2.0 4.0
5 1.0 4.0
6 1.0 4.0
在[45]中:df.diff() .abs()。ne([1,4])
出[45]:
Col 1 Col2
0 True True
1 False False
2 False False
3 True True
4 True False
5 False False
6 False False
在[46]中:df.diff()。abs()。ne([1,4])。any(1)
Out [46]:
0 True
1 False
2 False
3 True
4 True
5 False
6 False
dtype:bool
在[47]中:df.diff()。abs()。ne([1,4])。any(1).cumsum()
输出[47]:
0 1
1 1
2 1
3 2
4 3
5 3
6 3
dtype:int32
I'm curently learnig python and pandas (this question is based on a pevious post but with an additional query); at the moment have the 2 columns containing numeric sequences (ascending and/or descending) as described below:
Col 1: (col1 numeric incrememt and/or decrement = 1)
1
2
3
5
7
8
9
Col 2: (Col2 numeric increment and/or decrement = 4)
113
109
105
90
94
98
102
Need to extract the numeric ranges from both columns and print them according to the sequence break occurance on any of those 2 columns and the result should be as follow:
1,3,105,113
5,5,90,90
7,9,94,102
Already received a very useful way to do it using python's pandas library by @MaxU where it generates the numeric ranges based on the breaks detected on both columns using a criteria of col1 and col2 = increase and/or decreases by 1.
How can I extract numeric ranges from 2 columns and print the range from both columns as tuples?
The unique difference on this case is that the increment/decrement criteria applied for both columns are different for each one of them.
Try this:
In [42]: df
Out[42]:
Col1 Col2
0 1 113
1 2 109
2 3 105
3 5 90
4 7 94
5 8 98
6 9 102
In [43]: df.groupby(df.diff().abs().ne([1,4]).any(1).cumsum()).agg(['min','max'])
Out[43]:
Col1 Col2
min max min max
1 1 3 105 113
2 5 5 90 90
3 7 9 94 102
Explanation: our goal is to group those rows with the increment/decrement [1,4]
for Col1
, Col2
correspondingly:
In [44]: df.diff().abs()
Out[44]:
Col1 Col2
0 NaN NaN
1 1.0 4.0
2 1.0 4.0
3 2.0 15.0
4 2.0 4.0
5 1.0 4.0
6 1.0 4.0
In [45]: df.diff().abs().ne([1,4])
Out[45]:
Col1 Col2
0 True True
1 False False
2 False False
3 True True
4 True False
5 False False
6 False False
In [46]: df.diff().abs().ne([1,4]).any(1)
Out[46]:
0 True
1 False
2 False
3 True
4 True
5 False
6 False
dtype: bool
In [47]: df.diff().abs().ne([1,4]).any(1).cumsum()
Out[47]:
0 1
1 1
2 1
3 2
4 3
5 3
6 3
dtype: int32
这篇关于如何从2列包含数字序列中提取数字范围,并打印两列的范围(不同的增量值)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!