Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Numpy structured array
#1
Hi all,

I am fairly new to Python and teaching myself. I am working on importing data from a txt file into a NumPy structured array. I have loaded the data in to give three fields: the IATA code of an airport, the airport's latitude, and it's longitude...

 import numpy as np

fname = 'busiest_airports.txt'

dtype1 = np.dtype([('IATA', '|S4'), ('latitude', 'f8'),
                   ('longitude', 'f8')])

a = np.loadtxt(fname, dtype = dtype1, usecols = (0, -2, -1))

print(a) 

This gives me the following array...

 
array([(b'FRA',  50.026,    8.543), (b'DEN',  39.862, -104.673),
       (b'BKK',  13.681,  100.747), (b'SYD', -33.946,  151.177),
       (b'GRU', -23.432,  -46.47 ), (b'CLT',  35.214,  -80.943),
       (b'SFO',  37.619, -122.375), (b'IAH',  29.984,  -95.341),
       (b'ATL',  33.637,  -84.428), (b'ICN',  37.469,  126.451),
       (b'DFW',  32.897,  -97.038), (b'HND',  35.552,  139.78 ),
       (b'LAS',  36.08 , -115.152), (b'PHX',  33.434, -112.012),
       (b'PVG',  31.143,  121.805), (b'DXB',  25.253,   55.364),
       (b'MIA',  25.793,  -80.291), (b'IST',  40.977,   28.815),
       (b'AMS',  52.309,    4.764), (b'CAN',  23.392,  113.299),
       (b'LAX',  33.943, -118.408), (b'CGK',  -6.126,  106.656),
       (b'LHR',  51.477,   -0.461), (b'JFK',  40.64 ,  -73.779),
       (b'SIN',   1.35 ,  103.994), (b'CDG',  49.013,    2.55 ),
       (b'HKG',  22.309,  113.915), (b'ORD',  41.979,  -87.905),
       (b'KUL',   2.746,  101.71 ), (b'PEK',  40.08 ,  116.585)],
      dtype=[('IATA', 'S4'), ('latitude', '<f8'), ('longitude', '<f8')]) 
Now I want to write a function so that I can call the IATA code of an airport and have it's latitude returned. I have tried
 
 def get_lat(x):
    x = a['IATA'][i]
    latitude = a['latitude'][i]
    return latitude 
The idea was to try to set the index 'i' depending on which IATA code is input as the argument of the function. For example an argument of 'BKK' would set i = 1. However I get the error message that 'i' is not defined. Does anyone know how to correctly define the function? Thanks in advance
Reply
#2
You are not structuring the array as you think.
You are creating an array that is 30x1. Verify this by
print(a.shape())
Each element in the array is an object, since numpy arrays can only hold one "type".
If you
print(a[0], type(a[0]))
you will get (b'FRA', 50.026, 8.543) and numpy.void as the type.

Pandas, on the other hand, supports arrays that have multiple types and should handle this problem better.
Reply
#3
Thanks for the reply. I probably should have mentioned that this is an exercise from a textbook I am studying that has instructions to complete the task specifically using structured arrays. Can you think of any way of doing it without Pandas?
Reply
#4
OK. Not as a function but this does the trick, modify as needed at accept an argument and as a function and you've got it.
import numpy as np

a = np.array([(b'FRA',  50.026,    8.543), (b'DEN',  39.862, -104.673),
       (b'BKK',  13.681,  100.747), (b'SYD', -33.946,  151.177),
       (b'GRU', -23.432,  -46.47 ), (b'CLT',  35.214,  -80.943),
       (b'SFO',  37.619, -122.375), (b'IAH',  29.984,  -95.341),
       (b'ATL',  33.637,  -84.428), (b'ICN',  37.469,  126.451),
       (b'DFW',  32.897,  -97.038), (b'HND',  35.552,  139.78 ),
       (b'LAS',  36.08 , -115.152), (b'PHX',  33.434, -112.012),
       (b'PVG',  31.143,  121.805), (b'DXB',  25.253,   55.364),
       (b'MIA',  25.793,  -80.291), (b'IST',  40.977,   28.815),
       (b'AMS',  52.309,    4.764), (b'CAN',  23.392,  113.299),
       (b'LAX',  33.943, -118.408), (b'CGK',  -6.126,  106.656),
       (b'LHR',  51.477,   -0.461), (b'JFK',  40.64 ,  -73.779),
       (b'SIN',   1.35 ,  103.994), (b'CDG',  49.013,    2.55 ),
       (b'HKG',  22.309,  113.915), (b'ORD',  41.979,  -87.905),
       (b'KUL',   2.746,  101.71 ), (b'PEK',  40.08 ,  116.585)],dtype=[('IATA', 'S4'), ('latitude', '<f8'), ('longitude', '<f8')])

for obj in a:
    better_list = list(obj)
    if better_list[0] == b'BKK' :
        print(f"Foo = {better_list[1]}")
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Numpy] How to store different data type in one numpy array? water 7 287 Mar-26-2024, 02:18 PM
Last Post: snippsat
  reshaping 2D numpy array paul18fr 3 969 Jan-03-2023, 06:45 PM
Last Post: paul18fr
  Numpy returns "TypeError: unsupported operand type(s) for *: 'numpy.ufunc' and 'int'" kalle 2 2,527 Jul-19-2022, 06:31 AM
Last Post: paul18fr
  Numpy array BrianPA 13 4,836 Jan-23-2021, 09:36 AM
Last Post: Serafim
  How to fill datetime64 field in numpy structured array? AlekseyPython 0 2,234 Oct-20-2020, 08:17 AM
Last Post: AlekseyPython
  Adding data in 3D array from 2D numpy array asmasattar 0 2,168 Jul-23-2020, 10:55 AM
Last Post: asmasattar
  converting dataframe to int numpy array glennford49 1 2,290 Apr-04-2020, 06:15 AM
Last Post: snippsat
  Replacing sub array in Numpy array ThemePark 5 4,086 Apr-01-2020, 01:16 PM
Last Post: ThemePark
  How to prepare a NumPy array which include float type array elements subhash 0 1,884 Mar-02-2020, 06:46 AM
Last Post: subhash
  numpy.where array search for string in just one coordinate adetheheat 1 2,246 Jan-09-2020, 07:09 PM
Last Post: paul18fr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020