[Python] Is there a simpler way to write this Numpy structured array query?

Discussão em 'Python' iniciado por Stack, Março 18, 2025.

  1. Stack

    Stack Membro Participativo

    This Python 3.12 script

    import numpy as np

    a = np.array([('Rex', 9, 18), ('Fido', 3, 22), ('Fido', 7, 42), ('Fluffy', 1, 30), ('Fido', 5, 19)],
    dtype=[('name', 'U10'), ('age', 'f4'), ('weight', 'f4')])

    b = a[np.where((a['name'] == 'Fido') & (a['weight'] < 30))]
    oldestFidoUnder30 = b[np.argmax(b['age'])]


    looks for the oldest dog named Fido and weighing less than 30 units. Is there a simpler way to write this query? Would it be simpler or more readable with Pandas or another data frame library?

    Continue reading...

Compartilhe esta Página