How to sort numpy array by absolute value of a column?
<p>What I have now:</p>

<pre><code>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")
</code></pre>

<p>I would like:</p>

<ul>
<li>A solution for 3): To be able to add this new "abs" column to the original ndarray or</li>
<li>Another approach to be able to sort a csv file by the absolute values of a column.</li>
</ul>

<p>Here's a solution using <a href="http://pandas.pydata.org/" rel="nofollow">pandas</a>:</p>

<pre><code>In [117]: import pandas as pd

In [118]: df = pd.read_csv('test.csv')

In [119]: df
Out[119]: 
   a  b
0  1 -3
1  2  2
2  3 -1
3  4  4

In [120]: df['c'] = abs(df['b'])

In [121]: df
Out[121]: 
   a  b  c
0  1 -3  3
1  2  2  2
2  3 -1  1
3  4  4  4

In [122]: df.sort_values(by='c')
Out[122]: 
   a  b  c
2  3 -1  1
1  2  2  2
0  1 -3  3
3  4  4  4
</code></pre>

python python-3.x csv numpy