Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sort numpy array by absolute value of a column?

What I have now:

import numpy as np
# 1) Read CSV with headers
data = np.genfromtxt("big.csv", delimiter=',', names=True)
# 2) Get absolute values for column in a new ndarray
new_ndarray = np.absolute(data["target_column_name"])
# 3) Append column in new_ndarray to data
# I'm having trouble here. Can't get hstack, concatenate, append, etc; to work
# 4) Sort by new column and obtain a new ndarray
data.sort(order="target_column_name_abs")

I would like:

  • A solution for 3): To be able to add this new "abs" column to the original ndarray or
  • Another approach to be able to sort a csv file by the absolute values of a column.
like image 973
Alechan Avatar asked Aug 25 '16 19:08

Alechan


1 Answers

Here is a way to do it.
First, let's create a sample array:

In [39]: a = (np.arange(12).reshape(4, 3) - 6)

In [40]: a
Out[40]: 
array([[-6, -5, -4],
       [-3, -2, -1],
       [ 0,  1,  2],
       [ 3,  4,  5]])

Ok, lets say

In [41]: col = 1

which is the column we want to sort by,
and here is the sorting code - using Python's sorted:

In [42]: b = sorted(a, key=lambda row: np.abs(row[col]))

Let's convert b from list to array, and we have:

In [43]: np.array(b)
Out[43]: 
array([[ 0,  1,  2],
       [-3, -2, -1],
       [ 3,  4,  5],
       [-6, -5, -4]])

Which is the array with the rows sorted according to
the absolute value of column 1.

like image 164
Israel Unterman Avatar answered Nov 11 '22 18:11

Israel Unterman