usernumber
usernumber

Reputation: 2186

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

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?

Upvotes: 0

Views: 614

Answers (1)

Iguananaut
Iguananaut

Reputation: 23306

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)

Upvotes: 2

Related Questions