Skip to content Skip to sidebar Skip to footer

How Can I Extract Numeric Ranges From 2 Columns And Print The Range From Both Columns As Tuples?

I'm quite new on bash scripting and on python programing; at the moment have 2 columns which contains numeric sequences as follow: Col 1: 1 2 3 5 7 8 Col 2: 101 102 103 105 107 1

Solution 1:

UPDATE:

In [103]: df
Out[103]:
   Col1  Col2
0     7    52
1     8    51
2     9    47
3    10    46
4    11    45

In [104]: (df.groupby((df.diff().abs() != 1).any(1).cumsum()).agg(['min','max']))
Out[104]:
  Col1     Col2
   min max  min max
1    7   8   51  52
2    9  11   45  47

OLD answer:

Here is one way (among many) to do it in Pandas:

Data:

In [314]: df
Out[314]:
   Col1  Col2
0     1   101
1     2   102
2     3   103
3     5   105
4     8   108
5     7   107
6     6   106
7     9   109

NOTE: pay attention - rows with indexes (4,5,6) is a descending sequence

Answer :

In [350]: rslt = (df.groupby((df.diff().abs() != 1).all(1).cumsum())
     ...:           .agg(['min','max']))
     ...:

In [351]: rslt
Out[351]:
  Col1     Col2
   min max  min  max
1    1   3  101  103
2    5   5  105  105
3    6   8  106  108
4    9   9  109  109

now you can easily save it to CSV file:

rslt.to_csv(r'/path/to/file_name.csv', index=False, header=None)

or just print it:

In [333]: print(rslt.to_csv(index=False, header=None))
1,3,101,103
5,5,105,105
6,8,106,108
9,9,109,109

Post a Comment for "How Can I Extract Numeric Ranges From 2 Columns And Print The Range From Both Columns As Tuples?"