4️⃣ 🔗 NumPy Array Operations – Join, Split, Search, Sort & Filter
🧲 Introduction – Why Learn Array Operations?
Once you’ve created and accessed arrays in NumPy, the next step is mastering array operations like joining, splitting, searching, sorting, and filtering. These operations help you transform and analyze large datasets efficiently—essential skills in data science, ML preprocessing, and scientific computing.
🎯 In this guide, you’ll learn:
- How to join and split arrays with
concatenate()
andsplit()
- How to search and filter arrays based on conditions
- How to sort arrays for organized data processing
📘 Topics Covered
🔖 Topic | 📄 Description |
---|---|
➕ NumPy Array Join | Combine arrays using concatenate() , stack() , hstack() |
✂️ NumPy Array Split | Split arrays using split() , hsplit() , vsplit() |
🔍 NumPy Array Search | Find element positions with where() and searchsorted() |
🔃 NumPy Array Sort | Sort arrays using sort() or argsort() |
🧪 NumPy Array Filter | Filter data using boolean indexing and conditions |
➕ NumPy Array Join
You can merge arrays with functions like concatenate()
and stack()
.
import numpy as np
a = np.array([1, 2])
b = np.array([3, 4])
print(np.concatenate((a, b))) # [1 2 3 4]
Other Joining Functions:
np.stack()
– adds a new axisnp.hstack()
– horizontal stacknp.vstack()
– vertical stack
✂️ NumPy Array Split
Break large arrays into multiple parts using split()
.
arr = np.array([10, 20, 30, 40])
newarr = np.array_split(arr, 2)
print(newarr) # [array([10, 20]), array([30, 40])]
Other Splitting Functions:
np.hsplit()
– splits horizontallynp.vsplit()
– splits vertically (for 2D arrays)
🔍 NumPy Array Search
Use where()
to find index locations of elements:
arr = np.array([4, 6, 8, 6])
result = np.where(arr == 6)
print(result) # (array([1, 3]),)
📌 Use searchsorted()
for sorted arrays:
arr = np.array([10, 20, 30])
print(np.searchsorted(arr, 25)) # Output: 2
🔃 NumPy Array Sort
Sort elements in ascending order:
arr = np.array([3, 1, 2])
print(np.sort(arr)) # [1 2 3]
🔹 For descending order:
print(np.sort(arr)[::-1]) # [3 2 1]
🧪 NumPy Array Filter
Use boolean indexing for filtering:
arr = np.array([5, 10, 15, 20])
filter_arr = arr > 10
print(arr[filter_arr]) # [15 20]
📌 Combine conditions:
print(arr[(arr > 5) & (arr < 20)]) # [10 15]
📌 Summary – Recap & Next Steps
NumPy offers powerful operations to manipulate and analyze array data quickly. Joining, splitting, sorting, searching, and filtering are fundamental for transforming raw data into actionable formats.
🔍 Key Takeaways:
- Use
concatenate()
orstack()
to merge arrays - Split arrays with
split()
orhsplit()
/vsplit()
- Search with
where()
orsearchsorted()
- Filter using boolean masks
⚙️ Real-World Relevance:
These array operations are vital in machine learning pipelines, statistical analysis, and matrix algebra across Python-based projects.
❓ FAQ – NumPy Array Operations
❓ How do I join two arrays?
✅ Use np.concatenate()
or np.stack()
to combine arrays.
❓ Can I split an array into unequal parts?
✅ Yes, use np.array_split()
for flexible splitting.
❓ How do I find the index of a value?
✅ Use np.where(array == value)
.
❓ What’s the difference between sort()
and argsort()
?
✅ sort()
returns sorted values; argsort()
returns the indices that would sort the array.
❓ How do I filter an array by condition?
✅ Use boolean indexing like arr[arr > 10]
.
Share Now :