I want to extract the max value of a table that has 100 rows: Something that can be poorly achieved like this:
maxA = max(data['a1size'].max(), data['a2size'].max(), data['a3size'].max()... and continuing like this until a100size.
Of course that solution will be not ideal at all.
Is there a better approach and more elegant than using a for
loop?
I'm looking for some kind of ..
operator. Something like:
maxA = max(data['a1size'].max() .. data['a100size'].max())
CodePudding user response:
The loc
operator allows you to use slicing by column names:
maxA = data.loc[:, 'a1size':'a100size'].values.max()
Note that in contrast to core Python slicing, the final column is included here. The first :
specifies that you want all the rows.
This works if the columns you want to take the maximum over are contiguous. Otherwise, you could index the dataframe with a list of column names instead. To construct such a list, you could use a list comprehension:
a_size_cols = [f'a{i}size' for i in range(1, 101)]
maxA = data[a_size_cols].values.max()