Querying Numpy as if it were a database

import numpy as np cols = ['name', 'age', 'address', 'country'] A = np.array([ ['Courtney', 22, '8453 E. Coffee St., Beltsville, MD 20705', 'USA'], ['Huey', 33, '9136 Marlborough Street, Longview, TX 75604', 'USA'], ['Trudie', 25, '15 Sycamore St., Kennewick, WA 99337', 'USA'], ['Marion', 27, '827 King Street, Jonesborough, TN 37659', 'USA'], ['Lesley', 29, '199 Ann Ave., Hinesville, GA 31313', 'USA'], ['Beckie', 21, '315 Birch Hill St., El Dorado, AR 71730', 'USA'], ['Ricardo', 38, '48 Old Jennings Dr., Woonsocket, RI 02895', 'USA'], ['Willa', 26, '373 Stonybrook Ave., Loveland, OH 45140', 'USA'], ['Becki', 25, '9101 South Columbia Street, Stow, OH 44224', 'USA'], ['Roxann', 26, '7745 North Shub Farm Street, Ellicott City, MD 21042', 'USA'], ['Seymour', 23, '98 Mulberry St., Dyersburg, TN 38024', 'USA'], ['Sophia', 24, '7128 Shirley Lane, Lawrence Township, NJ 08648', 'USA'], ['Robbie', 29, '799 Manhattan St., Woburn, MA 01801', 'USA'], ['Loyd', 30, '627 W. Wentworth Ave., Westport, CT 06880', 'USA'], ['Margart', 24, '56 Oklahoma Lane, Dubuque, IA 52001', 'USA'], ['Hyo', 36, '533 Goldfield Street, Cookeville, TN 38501', 'USA'], ['Shirley', 31, '24 Water Avenue, San Antonio, TX 78213', 'USA'], ['Farah', 26, '84th Dr., Fairmont, WV 26554', 'USA'], ['Rhonda', 27, '12 Circle Street, Floral Park, NY 11001', 'USA'], ['Ricarda', 29, '7597 Second Street, Roanoke Rapids, NC 27870', 'USA'] ]) # What do we know about Sophia? print(A[A[:, cols.index('name')] == 'Sophia'][:, 1:]) # [['24' '7128 Shirley Lane, Lawrence Township, NJ 08648' 'USA']] # Who lives on Wentworth Ave? print(A[['Wentworth Ave' in addr for addr in A[:, cols.index('address')]]][0, 0]) # Loyd # Who lives in El Dorado? print(A[['El Dorado' in addr for addr in A[:, cols.index('address')]]][0, 0]) # Beckie # Who is older than 26, but younger than 33? age = A[:, cols.index('age')].astype(int) print(A[(26 < age) & (age < 33)][:, cols.index('name')]) # ['Marion' 'Lesley' 'Robbie' 'Loyd' 'Shirley' 'Rhonda' 'Ricarda'] # How old is Trudie? print(A[A[:, cols.index('name')] == 'Trudie'][:, cols.index('age')][0]) # 25 # Can you group the people by age? print(sorted({uniq_age: list(A[:, 0][A[:, 1].astype(int) == uniq_age]) for uniq_age in {int(age) for age in A[:, 1]}}.items(), key=lambda x: x[0])) #[(21, ['Beckie']), (22, ['Courtney']), (23, ['Seymour']), (24, ['Sophia', 'Margart']), (25, ['Trudie', 'Becki']), (26, ['Willa', 'Roxann', 'Farah']), (27, ['Marion', 'Rhonda']), (29, ['Lesley', 'Robbie', 'Ricarda']), (30, ['Loyd']), (31, ['Shirley']), (33, ['Huey']), (36, ['Hyo']), (38, ['Ricardo'])] # In which cities in TN, USA do people live? print([addr.split(',')[1].strip() for addr, country in A[:, cols.index('address'):] if 'TN' in addr and country == 'USA']) # ['Jonesborough', 'Dyersburg', 'Cookeville']