Python for Data Science: Chapter 5: NumPy and Pandas Libraries

NumPy: Basic array Operations

Python library

Basic Operations - NumPy (Python library) : 1. Join 2. Split 3. Search 4. Sort

Basic Operations


1. Join

•  Joining means combining two or more arrays into a single array. In NumPy we can combine the arrays using the functions like concatenate(), stack(), hstack() or vstack().

• The np.concatenate() function is used to join two arrays. The concatenation of the two arrays can be done on axis = 0 (default) or axis = 1. The concept of axis is illustrated by following example


• Following code illustrates the use of concatenate function in numpy‒

In [20]: import numpy as np

a = np.array([[1,2,3],[4,5,6]])

print('First array:')

print(a)

b = np.array([[10,20,30], [40, 50, 60]])

print('Second array:')

print(b)

# both the arrays are of same dimensions

print("Joining the two arrays along axis 0:')

print(np.concatenate((a,b)))

print('Joining the two arrays along axis 1:')

print(np.concatenate ((a,b), axis = 1))

First array:

[[1 2 3]

 [4 5 6]]

second array:

 [[10 20 30]

 [40 50 60]]

Joining the two arrays along axis 0 :

[[1 2 3]

 [ 4 5 6]

 [10 20 30]

 [40 50 60]]

Joining the two arrays along axis 1:

[[ 1 2 3 10 20 30]

 [ 4 5 6 40 50 60]]

Python program for vertical join of two dimensional arrays

import numpy as np

arr1 = np.array([[10,20], [30,40]])

arr2 = np.array([[50,60],[70,80]])

result = = np.vstack((arr1,arr2))

print("Vertical Join: ",result)

Output

Vertical Join: [[10 20]

[30 40]

[50 60]

[70 80]]

Python program for horizontal join of two dimensional arrays

import numpy as np

arr1 = np.array([[10,20],[30,40]])

arr2 = np.array([[50,60],[70,80]])

result = np.hstack((arr1, arr2))

print("Horizontal Join: ",result)

Output

Horizontal Join: [[10 20 50 60]

 [30 40 70 80]]


2. Split

• Split operation divides one array into multiple smaller arrays. There are following functions from NumPy by which the array can be split

np.split(): The np.split() function in NumPy is used to divide one array into multiple equal sub‒arrays. The syntax is

np.split(array,n)

where array is an array which we want to split and n is the number of equal parts.

For example ‒

Python program

import numpy as np

arr = np.array([10,20,30,40,50,60])

result = np.split(arr,3)

print(result)

Output

 [array([10, 20]), array([30, 40]), array([50, 60])]

The above code splits the array into 3 equal parts and each part gets 2 elements.

np.array_split() : The array_split() allows uneven splitting when the array length isn't divisible by the number of parts. For an array that causes uneven split, the np.split( ) raises error but the np.array.split() works fine.

For example ‒

Python program

import numpy as np

am = np.array([10,20,30,40,50])

result = np.array_split(arr,3)

print(result)

Output

[array([10, 20]), array([30, 40]), array([50])]

np.hsplit() : The horizontal split means you are cutting the array vertically (along the columns). After this split, we end up with chunks of columns.


Python program

import numpy as np

arr = np.array([[1,2,3,4],

[5,6,7,8],

[9,10,11,12],

[13,14,15,16]])

print(arr,"\n")

h_parts = np.hsplit(arr, 2)

count = 1

for part in h_parts:

   print("Part", count, ":\n", part, "\n")

   count += 1

Output

[[ 1 2 3 4]

 [ 5 6 7 8]

 [ 9 10 11 12]

 [13 14 15 16]]

Part 1:

 [[ 1 2 ]

  [ 5 6 ]

 [ 9 10]

 [13 14]]

Part 2 :

[[ 3 4]

 [ 7 8]

 [11 12]

 [15 16]]

np.vsplit(): The vertical split means you are cutting the array horizontally(along rows) and we end up with chunks of rows

 Horizontal splits results in vertical Parts.


Python program

import numpy as np

arr = np.array([[1,2,3,4],

[5,6,7,8],

[9,10,11,12],

[13,14,15,16]])

print(arr, "\n")

v_parts = np.vsplit(arr, 2)

count = 1

for part in v_parts:

    print("Part", count, ":\n", part, "\n")

    count += 1

Output

[[ 1 2 3 4]

[ 5 6 7 8]

 [ 9 10 11 12]

[13 14 15 16]]

Part 1:

[[1 2 3 4]

[5 6 7 8]]

Part 2:

[[ 9 10 11 12]

[13 14 15 16]]

 

3. Search

•   Searching means finding the index (position) of elements that match a condition or value. The function np.where() returns indexes where condition is true. Following program illustrates the use of this function

Python program

import numpy as np

arr = np.array([10,20,30,40,20,50,20])

result = np.where(arr==20)

print("Index position of 20:",result)

Output

Index position of 20: (array([1, 4, 6], dtype=int64),)

It means, position of element 20 in the array is at index 1,4 and 6. Hence is the output.

 

4. Sort

• Sorting means rearranging elements of an array in ascending or descending order. The function np.sort(array) is used to sort the array elements. Following program illustrates the use of this function ‒

Python program

import numpy as np

arr = np.array([30,40,10,20,50])

result = np.sort(arr)

print(result)

Output

[10 20 30 40 50]

 

Python for Data Science: Chapter 5: NumPy and Pandas Libraries : Tag: Computer Programming, Python, Data Science : Python library - NumPy: Basic array Operations


Python for Data Science: Chapter 5: NumPy and Pandas Libraries



Under Subject


Python for Data Science

AD25201 2nd Semester AIDS Dept | 2025 Regulation | 2nd Semester 2025 Regulation



Related Subjects


English Essentials II

EN25C02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation



Linear Algebra

MA25C02 2nd Semester | 2025 Regulation


Applied Physics (CSIE) II

PH25C03 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Digital Principles and Computer Organization

CS25C06 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Basic Electrical and Electronics Engineering

EE25C01 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation


Python for Data Science

AD25201 2nd Semester AIDS Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Re-Engineering for Innovation

ME25C05 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation


Python for Data Science - Laboratory

AD25201 2nd Semester AIDS Dept | 2025 Regulation | 2nd Semester 2025 Regulation