Python – ‌How to select rows with minimum value in each group based on first column as ID

awkperlpythontext processing

I have a file looks like this :

1   7.8e-12  
1   7.8e-12  
1   1.0e-11   
2   9.3e-13    
2   3.5e-12 
2   3.5e-10
2   3.1e-9         
3   3.0e-11    
3   3.0e-11     
3   1.7e-08   

For every value in column one, I want to select "all rows" having minimum value in column 2 and group by column one. So the desired output is:

 1   7.8e-12  
 1   7.8e-12
 2   9.3e-13
 3   3.0e-11    
 3   3.0e-11 

Any idea how to do this?

Best Answer

One approach would be to sort in ascending order, then note the first col2 value for each col1 and print if the current col2 value is equal to it:

sort -k1,1n -k2,2g file | awk '!a[$1] {a[$1] = $2} $2 == a[$1]'
1   7.8e-12
1   7.8e-12
2   9.3e-13
3   3.0e-11
3   3.0e-11
Related Question