合并按值分组的2d numpy数组

时间:2018-10-13 16:43:58

标签: python numpy numpy-ndarray

我有这个数组:

np = "8 12 16 20 24 28 32"
do for [i=1:7] {
    plot for [IDX=0:4] sprintf("run-1/%s.t-0.dat", word(np, i)) ...

第一个索引包含动物的名称,第二个索引包含动物的名称,第三个索引是种群。我需要获取每个区域中物种的平均值,并获取每个区域中每种物种的最大值和最小值。因此,对于“紫色海雀”,平均值应为(125 + 143 + 128)/ 3 = 132

我对如何获取numpy数组仅计算每个区域的人口感到非常困惑。

将2d数组分为多个2d数组会更好还是更容易?

2 个答案:

答案 0 :(得分:3)

这看起来更像是熊猫的任务,我们可以先构建一个数据框:

import pandas as pd

df = pd.DataFrame([
    ['Burgundy Bichon Frise','1','137'],
    ['Pumpkin Pomeranian','1','182'],
    ['Purple Puffin','1','125'],
    ['Wisteria Wombat','1','109'],
    ['Burgundy Bichon Frise','2','168'],
    ['Pumpkin Pomeranian','2','141'],
    ['Purple Puffin','2','143'],
    ['Wisteria Wombat','2','167'],
    ['Burgundy Bichon Frise','3','154'],
    ['Pumpkin Pomeranian','3','175'],
    ['Purple Puffin','3','128'],
    ['Wisteria Wombat','3','167']], columns=['animal', 'region', 'n'])

接下来,我们可以将regionn转换为数字,这将使统计数据的计算更加容易:

df.region = pd.to_numeric(df.region)
df.n = pd.to_numeric(df.n)

最后,我们可以执行.groupby(..),然后计算总计,例如:

>>> df[['animal', 'n']].groupby(('animal')).min()
                         n
animal                    
Burgundy Bichon Frise  137
Pumpkin Pomeranian     141
Purple Puffin          125
Wisteria Wombat        109
>>> df[['animal', 'n']].groupby(('animal')).max()
                         n
animal                    
Burgundy Bichon Frise  168
Pumpkin Pomeranian     182
Purple Puffin          143
Wisteria Wombat        167
>>> df[['animal', 'n']].groupby(('animal')).mean()
                                n
animal                           
Burgundy Bichon Frise  153.000000
Pumpkin Pomeranian     166.000000
Purple Puffin          132.000000
Wisteria Wombat        147.666667

编辑:获取每只动物 的最小行

我们可以使用idxmin / idxmax获取每个动物的最小/最大行的索引号,然后使用df.iloc[..]获取这些行,例如:

>>> df.ix[df.groupby(('animal'))['n'].idxmin()]
                  animal  region    n
0  Burgundy Bichon Frise       1  137
5     Pumpkin Pomeranian       2  141
2          Purple Puffin       1  125
3        Wisteria Wombat       1  109
>>> df.ix[df.groupby(('animal'))['n'].idxmax()]
                  animal  region    n
4  Burgundy Bichon Frise       2  168
1     Pumpkin Pomeranian       1  182
6          Purple Puffin       2  143
7        Wisteria Wombat       2  167

0, 5, 2, 3(用于idxmin)是数据帧的“行号”。

答案 1 :(得分:2)

以下是使用numpy将数据a转换为2D表的方法:

>>> unqr, invr = np.unique(a[:, 0], return_inverse=True)
>>> unqc, invc = np.unique(a[:, 1], return_inverse=True)
# initialize with nans in case there are missing values
# these are then treated correctly by nanmean etc.:
>>> out = np.full((unqr.size, unqc.size), np.nan)
>>> out[invr, invc] = a[:, 2]
>>> 
# now we have a table
>>> out
array([[137., 168., 154.],
       [182., 141., 175.],
       [125., 143., 128.],
       [109., 167., 167.]])
# with rows
>>> unqr
array(['Burgundy Bichon Frise', 'Pumpkin Pomeranian', 'Purple Puffin',
       'Wisteria Wombat'], dtype='<U21')
# and columns
>>> unqc
array(['1', '2', '3'], dtype='<U21')
>>> 
# find the mean for 'Purple Puffin':
>>> np.nanmean(out[unqr.searchsorted('Purple Puffin')])
132.0
# find the max for region '2'
>>> np.nanmax(out[:, unqc.searchsorted('2')])
168.0