【问题标题】:How to sort numpy array by absolute value of a column?如何按列的绝对值对numpy数组进行排序?
【发布时间】:2017-01-02 08:12:24
【问题描述】:

我现在拥有的:

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")

我想要:

  • 3) 的解决方案:能够将此新的“abs”列添加到原始 ndarray 或
  • 另一种能够按列的绝对值对 csv 文件进行排序的方法。

【问题讨论】:

  • 它不工作的原因是形状不同。尝试 new_ndarray.shape() 并与 ata["target_column_name"].shape() 进行比较

标签: python python-3.x csv numpy


【解决方案1】:

这是一种方法。
首先,让我们创建一个示例数组:

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]])

好吧,让我们说

In [41]: col = 1

这是我们要排序的列,
这是排序代码 - 使用 Python 的 sorted:

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

让我们将 b 从列表转换为数组,我们有:

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

哪个是行排序依据的数组
第 1 列的绝对值。

【讨论】:

    【解决方案2】:

    这是使用pandas的解决方案:

    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
    

    【讨论】:

      猜你喜欢
      • 2020-12-23
      • 2021-09-21
      • 1970-01-01
      • 2011-02-19
      • 2021-05-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多