Warm tip: This article is reproduced from stackoverflow.com, please click
max astropy

Astropy: iterating over columns in table with a certain pattern in column name

发布于 2020-03-28 23:17:47

I have an astropy table t such that t.colnames shows something like:

['a', 'b', 'c', 'err_a', 'err_b', 'err_c', 'd', 'e', 'f']

I would like to determine for each row in the table the maximal value for the columns that have 'err' in the name.

I can do something like

for line in t:
    max = 0
    for col in t.colnames:
        if col[:3] == 'err':
            if line[col] > max:
                max = line[col]
    print(max)

Is there a simpler way to do this?

Questioner
usernumber
Viewed
18
Iguananaut 2020-01-31 18:54

Assuming you want the max value across all the "err" columns, you could do this:

max(t[c].max() for c in t.colnames if 'err' in c)

For the max per-row it's slightly trickier since the data in Astropy tables is typically column-oriented. It's probably simplest to convert the relevant columns to a Numpy array and broadcast the max function along the row axis; this is slightly trickier because you need to convert the array from a mixed-type to single type (assuming all the "err" columns are the same dtype, say, float64):

err_cols = [c for c in t.colnames if 'err' in c]
t.as_array(names=err_cols).view(dtype=float).reshape((len(t), len(err_cols))).max(axis=1)