current position:Home>Numpy common operations of Python data analysis series Chapter 8
Numpy common operations of Python data analysis series Chapter 8
2022-01-31 22:49:38 【Programmer Zhang Yixiao】
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
copyright notice
author[Programmer Zhang Yixiao],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/01/202201312249363111.html
The sidebar is recommended
- leetcode 1560. Most Visited Sector in a Circular Track(python)
- leetcode 1995. Count Special Quadruplets(python)
- How to program based on interfaces using Python
- leetcode 1286. Iterator for Combination(python)
- leetcode 1418. Display Table of Food Orders in a Restaurant (python)
- Python Matplotlib drawing histogram
- Python development foundation summary (VII) database + FTP + character coding + source code security
- Python modular package management and import mechanism
- Django serialization (II)
- Python dataloader error "dataloader worker (PID XXX) is killed by signal" solution
guess what you like
-
apache2. 4 + Django + windows 10 Automated Deployment
-
leetcode 1222. Queens That Can Attack the King(python)
-
leetcode 1387. Sort Integers by The Power Value (python)
-
Tiger sniffing 24-hour praise device, a case with a crawler skill, python crawler lesson 7-9
-
Python object oriented programming 01: introduction classes and objects
-
Baidu Post: high definition Python
-
Python Matplotlib drawing contour map
-
Python crawler actual combat, requests module, python realizes IMDB movie top data visualization
-
Python classic: explain programming and development from simple to deep and step by step
-
Python implements URL availability monitoring and instant push
Random recommended
- Python avatar animation, come and generate your own animation avatar
- leetcode 1884. Egg Drop With 2 Eggs and N Floors(python)
- leetcode 1910. Remove All Occurrences of a Substring(python)
- Python and binary
- First acquaintance with Python class
- [Python data collection] scrapy book acquisition and coding analysis
- Python crawler from introduction to mastery (IV) extracting information from web pages
- Python crawler from entry to mastery (III) implementation of simple crawler
- The apscheduler module in Python implements scheduled tasks
- 1379. Find the same node in the cloned binary tree (Java / C + + / Python)
- Python connects redis, singleton and thread pool, and resolves problems encountered
- Python from 0 to 1 (day 11) - Python data application 1
- Python bisect module
- Python + OpenGL realizes real-time interactive writing on blocks with B-spline curves
- Use the properties of Python VTK implicit functions to select and cut data
- Learn these 10000 passages and become a humorous person in the IT workplace. Python crawler lessons 8-9
- leetcode 986. Interval List Intersections(python)
- leetcode 1860. Incremental Memory Leak(python)
- How to teach yourself Python? How long will it take?
- Python Matplotlib drawing pie chart
- Django paging (II)
- Concurrent. For Python concurrent programming Futures or multiprocessing?
- Programmers over the age of 25 can't know a few Chinese herbal medicines. Python crawler lessons 9-9
- Python crawler from introduction to pit full series of tutorials (detailed tutorial + various practical combat)
- The second bullet of class in Python
- Python object oriented programming 03: class inheritance and its derived terms
- How IOS developers learn Python Programming 13 - function 4
- Python crawler from introduction to mastery (VI) form and crawler login
- Python crawler from entry to mastery (V) challenges of dynamic web pages
- Deeply understand pandas to read excel, TXT, CSV files and other commands
- Daily python, Chapter 18, class
- "I just want to collect some plain photos in Python for machine learning," he said. "I believe you a ghost!"
- Django view
- Python implements filtering emoticons in text
- When winter comes, python chooses a coat with temperament for mom! Otherwise, there's really no way to start!
- Python crawler - get fund change information
- Highlight actor using Python VTK
- Python crawler actual combat: crawling southern weekend news articles
- leetcode 406. Queue Reconstruction by Height(python)
- leetcode 1043. Partition Array for Maximum Sum (python)