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
0752185129473104641145

In [104]: (df.groupby((df.diff().abs() != 1).any(1).cumsum()).agg(['min','max']))
Out[104]:
  Col1     Col2
   min max  min max
178515229114547

OLD answer:

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

Data:

In[314]: dfOut[314]:
   Col1Col20110112102231033510548108571076610679109

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
113101103255105105368106108499109109

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,1035,5,105,1056,8,106,1089,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?"