# Numpy common operations of Python data analysis series Chapter 8

2022-01-31 22:49:38

This is my participation 11 The fourth of the yuegengwen challenge 18 God , Check out the activity details ：2021 One last more challenge

Numpy A structured array similar to Python Medium class The concept of , It consists of a set of field names with specific meanings and specific data types ：

``````In [1]: import numpy as np

In [2]: data = np.array([('Mike', 18, 'SZ'), ('Jerry', 19, 'BJ')
...: ], dtype=[('name', 'U5'), ('age', 'i4'), ('city', 'U2')]
...: )

In [3]: data
Out[3]:
array([('Mike', 18, 'SZ'), ('Jerry', 19, 'BJ')],
dtype=[('name', '<U5'), ('age', '<i4'), ('city', '<U2')])
Copy code ``````

data Is a with two elements of length 2 One dimensional array of , Each element has the following three fields ：

``````name:  Length less than or equal to 5 Character string
age:  The length is 4 An integer of bytes
city:  Length less than or equal to 2 Character string
Copy code ``````

For the acquisition of structured arrays , You can use regular integer indexes to get data , You can also obtain data directly according to the field name ( The effect is equivalent to obtaining excel A column of data in ):

``````In [3]: data
Out[3]:
array([('Mike', 18, 'SZ'), ('Jerry', 19, 'BJ')],
dtype=[('name', '<U5'), ('age', '<i4'), ('city', '<U2')])

In [4]: data[0]
Out[4]: ('Mike', 18, 'SZ')

In [5]: data['city']
Out[5]: array(['SZ', 'BJ'], dtype='<U2')

In [6]: data['name']
Out[6]: array(['Mike', 'Jerry'], dtype='<U5')

In [7]: data['age']
Out[7]: array([18, 19], dtype=int32)

In [8]: data['city'][0]
Out[8]: 'SZ'
Copy code ``````

View the field name information of structured array ：

``````In [10]: data.dtype
Out[10]: dtype([('name', '<U5'), ('age', '<i4'), ('city', '<U2')])

In [12]: data.dtype.names
Out[12]: ('name', 'age', 'city')

In [13]: data.dtype.fields
Out[13]:
mappingproxy({'name': (dtype('<U5'), 0),
'age': (dtype('int32'), 20),
'city': (dtype('<U2'), 24)})
Copy code ``````

dtype.names Return the list of field names of this structured array ;dtype.fields Returns a field named key, The tuple of field data type and offset is used as key Dictionary like object .

Get the structured data by field name, and get the view of shared memory , Modifying the data of any array will affect all arrays sharing this memory ：

``````In [14]: names = data['name']

In [15]: names
Out[15]: array(['Mike', 'Jerry'], dtype='<U5')

In [16]: data
Out[16]:
array([('Mike', 18, 'SZ'), ('Jerry', 19, 'BJ')],
dtype=[('name', '<U5'), ('age', '<i4'), ('city', '<U2')])

In [17]: names[:] = 'Undefined'

In [18]: names
Out[18]: array(['Undef', 'Undef'], dtype='<U5')

In [19]: data
Out[19]:
array([('Undef', 18, 'SZ'), ('Undef', 19, 'BJ')],
dtype=[('name', '<U5'), ('age', '<i4'), ('city', '<U2')])
Copy code ``````