Header Ads Widget

⚡ Premium Tools Hub • EXE Apps + Full Python Source Code
Lite • Pro • Bundle Packs • Instant Download

NumPy Creating Structured Arrays Explained – Store Mixed Data Types in Python

NumPy Creating Structured Arrays

In real-world data, we often deal with different types of information together.

For example:

  • Name (string)
  • Age (integer)
  • Salary (float)

NumPy structured arrays allow us to store all these different types in a single array.


What are Structured Arrays?

Structured arrays are NumPy arrays with custom data types (fields).

Each element can have multiple named attributes, similar to a database record or table row.


Why Use Structured Arrays?

  • Store mixed data types
  • Easy data organization
  • Works like a mini database
  • Useful in data science
  • Efficient memory usage

1. Basic Structured Array Creation

import numpy as np

data = np.array([
("Alice", 25, 50000.0),
("Bob", 30, 60000.0),
("Charlie", 28, 55000.0)
], dtype=[("name", "U10"), ("age", "i4"), ("salary", "f4")])

print(data)

Output

[('Alice', 25, 50000.)
('Bob', 30, 60000.)
('Charlie', 28, 55000.)]

2. Understanding dtype (Data Types)

FieldTypeMeaning
name          U10                String (Unicode up to 10 chars)
age          i4                Integer
salary          f4                Float

3. Creating Empty Structured Array

import numpy as np

dtype = [("name", "U10"), ("age", "i4"), ("salary", "f4")]

data = np.zeros(3, dtype=dtype)

print(data)

Output

[('', 0, 0.) ('', 0, 0.) ('', 0, 0.)]

4. Creating Structured Array Using np.array()

import numpy as np

data = np.array([
("John", 40, 80000),
("Emma", 29, 65000)
], dtype=[("name", "U10"), ("age", "i4"), ("salary", "i4")])

print(data)

5. Creating Structured Array Using np.zeros()

import numpy as np

dtype = [("product", "U10"), ("price", "f4")]

products = np.zeros(3, dtype=dtype)

products["product"] = ["Pen", "Book", "Laptop"]
products["price"] = [1.5, 5.0, 1000.0]

print(products)

6. Creating Structured Array Using np.array with Dictionaries

import numpy as np

data = np.array([
{"name": "Alice", "age": 25, "salary": 50000},
{"name": "Bob", "age": 30, "salary": 60000}
])

print(data)

7. Accessing Structured Array Fields

print(data["name"])
print(data["age"])

8. Creating Structured Array for Students

import numpy as np

students = np.array([
("A", 90),
("B", 75),
("C", 88)
], dtype=[("name", "U10"), ("marks", "i4")])

print(students)

9. Updating Structured Array Values

students["marks"][0] = 95

print(students)

10. Real-World Example: Employee Records

import numpy as np

employees = np.array([
("John", 40, 80000),
("Emma", 29, 65000),
("Mike", 33, 72000)
], dtype=[("name", "U10"), ("age", "i4"), ("salary", "i4")])

print(employees)

11. Real-World Example: Product Inventory

import numpy as np

inventory = np.array([
("Laptop", 10, 900.0),
("Mouse", 50, 20.0),
("Keyboard", 30, 45.0)
], dtype=[("item", "U10"), ("stock", "i4"), ("price", "f4")])

print(inventory)

Key Features of Structured Arrays

FeatureDescription
dtype            Defines data structure
fields            Named columns
mixed types            string, int, float
indexing            field-based access

Advantages of Structured Arrays

  • Store multiple data types
  • Easy data management
  • Similar to database tables
  • Efficient memory usage
  • Useful in analytics and ML

Summary

NumPy structured arrays allow you to create and manage complex datasets with multiple data types in a single array. They behave like lightweight database tables and are very useful in data science.

This functionality is part of NumPy and is widely used in applications built with Python.


Conclusion

Creating structured arrays in NumPy is essential for handling real-world data. It simplifies data storage, access, and processing, making it a powerful tool for data science and machine learning.




Post a Comment

0 Comments