[Python] Create structured array (store heterogeneous data with NumPy)

0. Overview

You want to put together information about a person, such as name, age, and height. In that case, use NumPy's structured array. You can efficiently store different types of complex data. Since it's a big deal, I tried to compose it with Nogizaka members. (As of 02/24/2020)

1. Code

list.py


# -*- coding: utf-8
import numpy as np

#Store information of 4 people in each array
name = ['mai', 'asuka', 'erika', 'yoda']
age = [27, 21, 23, 19]
height = [162, 158, 160, 152]

#Creating a structured array(Determine the data type of 3 types of arrays)
data = np.zeros(4, dtype={'names' : ('name', 'age', 'height'), 'formats': ('U10', 'i4', 'f8')}) #U10 -> Unicode(Up to 10 characters), i4 -> int32, f8 -> float64

#Data type confirmation
print(data.dtype)
#[('name', '<U10'), ('age', '<i4'), ('height', '<f8')]

#Store data in a structured array
data['name'] = name
data['age'] = age
data['height'] = height

#Print all information of 4 people
print(data)
#[(u'mai', 27, 162.0) (u'asuka', 21, 158.0) (u'erika', 23, 160.0) (u'yoda', 19, 152.0)]

#Print the names of four people
print(data['name'])
#[u'mai' u'asuka' u'erika' u'yoda']

#Print all asuka information
print(data[1])
#(u'asuka', 21, 158.0)

#Print mai height
print(data[0]['height'])
#162.0

2. Reference books

O'Reilly Japan: Python Data Science Handbook

Recommended Posts

[Python] Create structured array (store heterogeneous data with NumPy)
Create a python numpy array
Create test data like that with Python (Part 1)
Data analysis with python 2
python numpy array calculation
[Python] Sorting Numpy data
Data analysis with Python
[python] Create a date array with arbitrary increments with np.arange
Sample data created with python
Artificial data generation with numpy
[Python] Calculation method with numpy
Create 3d gif with python3
Get Youtube data with python
Implemented SMO with Python + NumPy
Stumble story with Python array
Create a directory with python
Python3 | Getting Started with numpy
Read json data with python
Let's create a PRML diagram with Python, Numpy and matplotlib.
Create plot animation with Python + Matplotlib
Create Awaitable with Python / C API
Subscript access to python numpy array
Create folders from '01' to '12' with python
Create a virtual environment with Python!
[Python] Get economic data with DataReader
Create an Excel file with Python3
Python data structures learned with chemoinformatics
Python application: Numpy Part 3: Double array
Create noise-filled audio data with SoX
[Python] How to store a csv file as one-dimensional array data
Easy data visualization with Python seaborn.
Extract multiple elements with Numpy array
Process Pubmed .xml data with python
Data analysis starting with python (data visualization 1)
Data analysis starting with python (data visualization 2)
Python application: Data cleansing # 2: Data cleansing with DataFrame
Create a USB boot Ubuntu with a Python environment for data analysis
Python / numpy> Read the data file with the item name line> Use genfromtxt ()
Get additional data in LDAP with python
Receive textual data from mysql with python
Create a Python function decorator with Class
Automatically create Python API documentation with Sphinx
[Note] Get data from PostgreSQL with Python
Create wordcloud from your tweet with python3
Build a blockchain with Python ① Create a class
Process Pubmed .xml data with python [Part 2]
Add a Python data source with Redash
Create a dummy image with Python + PIL.
Retrieving food data with Amazon API (Python)
Try working with binary data in Python
Generate Japanese test data with Python faker
"Data Science 100 Knock (Structured Data Processing)" Python-007 Explanation
"Data Science 100 Knock (Structured Data Processing)" Python-006 Explanation
[Python] Create a virtual environment with Anaconda
Let's create a free group with Python
Quickly create an excel file with Python #python
Python / numpy> list (numpy array) file save / load
Convert Excel data to JSON with python
"Data Science 100 Knock (Structured Data Processing)" Python-001 Explanation
[Python] Use string data with scikit-learn SVM
Download Japanese stock price data with python