Program to merge two given NumPy arrays of identical shape

In this python numpy program, we will merge two given NumPy arrays of identical shape.

Steps to solve the program
  1. Import the numpy library as np.
  2. Create two identical arrays using np.array().
  3. Merge two given NumPy arrays using np.concatenate().
  4. Print the output.
				
					import numpy as np
x = np.array([1,2,3])
y = np.array([4,5,6])

print("Original arrays:")
print(x)
print(y)

output = np.concatenate((x, y))
print("\nAfter concatenate:")
print(output) 
				
			

Output :

				
					Original arrays:
[1 2 3]
[4 5 6]

After concatenate:
[1 2 3 4 5 6]
				
			

calculate averages without NaNs along a row.

Calculate averages without NaNs along a row of an array

In this python numpy program, we will calculate averages without NaNs along a row of an array.

Steps to solve the program
  1. Import the numpy library as np.
  2. Create an array using np.array().
  3. First, remove the NaN values using np.ma.masked_array()
  4. Calculate averages without NaN values along the rows using np.average() and set the axis equal to 1.
  5. Print the output.
				
					import numpy as np
x = np.array([[6, 5, np.nan],[np.nan, 9, 1],[7, 8, 2]])

temp = np.ma.masked_array(x,np.isnan(x))
print("Array after removing nan values: \n",temp)
print("\nAverages without NaNs along the said array:")
print(np.average(temp,axis=1))
				
			

Output :

				
					Array after removing nan values: 
 [[6.0 5.0 --]
 [-- 9.0 1.0]
 [7.0 8.0 2.0]]

Averages without NaNs along the said array:
[5.5 5.0 5.666666666666667]
				
			

calculate the sum of all columns of an array

merge two given arrays of identical shape.

Program to calculate the sum of all columns of the NumPy array

In this python numpy program, we will calculate the sum of all columns of the NumPy array.

Steps to solve the program
  1. Import the numpy library as np.
  2. Create an array using np.array().
  3. Calculate the sum of all columns of the given array using sum() and set axis equal to 0.
  4. Print the output.
				
					import numpy as np
x = np.array([[6, 5, 7],[10, 9, 1],[7, 8, 2]])

print("Sum of all columns: ",x.sum(axis=0))
				
			

Output :

				
					Sum of all columns:  [23 22 10]
				
			

copy data from a given array to another array.

calculate averages without NaNs along a row.

Program to copy data from a given array to another array

In this python numpy program, we will copy data from a given array to another array.

Steps to solve the program
  1. Import the numpy library as np.
  2. Create an array using np.array().
  3. Create an empty array of the same size as the given array.
  4. Use for loop to iterate over the given array and add all elements to the empty array.
  5. Print both arrays to see the output.
				
					import numpy as np
x = np.array([[6, 5, 7],[10, 9, 1],[7, 8, 2]])
y = [None] * len(x)
print(y)

for i in range(0,len(x)):
    y[i] = x[i]
    
print("Elements of original array: ");    
for i in range(0, len(x)):    
    print(x[i])    
     
print("Elements of new array: ");    
for i in range(0, len(y)):    
     print(y[i])  
				
			

Output :

				
					[None, None, None]
Elements of original array: 
[6 5 7]
[10  9  1]
[7 8 2]
Elements of new array: 
[6 5 7]
[10  9  1]
[7 8 2]
				
			

calculate the sum of all columns of an array

Extract the first and second elements of the rows from an array

In this python numpy program, we will extract the first and second elements of the rows from an array using NumPy.

Steps to solve the program
  1. Import the numpy library as np.
  2. Create an array using np.array().
  3. Extract the first and second elements of the first and second rows from the array using indexing.
  4. Print the output.
				
					import numpy as np
x = np.array([[6, 5, 7],[10, 9, 1],[7, 8, 2]])

print(x[0:2,0:2])
				
			

Output :

				
					[[ 6  5]
 [10  9]]
				
			

access first two columns of a 3-D array.

Program to access two columns of a NumPy array

In this python numpy program, we will access two columns of a NumPy array.

Steps to solve the program
  1. Import the numpy library as np.
  2. Create an array using np.array().
  3. Use for loop to iterate over the first two rows of the array.
  4. Access the first two columns of the array using indexing.
  5. Print the output.
				
					import numpy as np
x = np.array([[6, 5, 7],[10, 9, 1],[7, 8, 2]])

for i in range(0,2):
    print(f"{i+1} column: ",x[:,i])
				
			

Output :

				
					1 column:  [ 6 10  7]
2 column:  [5 9 8]
				
			

convert an array into a CSV file.

extract the first and second elements of the first and second rows from a given (3×3) matrix.

Program to convert a NumPy array into a CSV file

In this python numpy program, we will convert a NumPy array into a CSV file.

Steps to solve the program
  1. Import the numpy library as np.
  2. Import the pandas library as pd
  3. Create an array using np.array().
  4. Convert the array to a matrix form using reshape().
  5. Convert the matrix to a data-frame using pd.DataFrame().
  6. Now save this data-frame as csv file using to_csv().
  7. Read the saved csv file using pd.read_csv().
  8. Print the file.
				
					import pandas as pd
import numpy as np
 
x = np.arange(1,16).reshape(3,5)
 
DF = pd.DataFrame(x)
DF.to_csv("data1.csv")

df=pd.read_csv("data1.csv")
print(df)
				
			

Output :

				
					   Unnamed: 0   0   1   2   3   4
0           0   1   2   3   4   5
1           1   6   7   8   9  10
2           2  11  12  13  14  15
				
			

calculate the product of an array.

access first two columns of a 3-D array.

Program to calculate the product of a NumPy array

In this python numpy program, we will calculate the product of a NumPy array.

Steps to solve the program
  1. Import the numpy library as np.
  2. Create an array using np.array().
  3. Calculate the product of the given array using prod().
  4. Print the output.
				
					import numpy as np
x = np.array([3, 5, 1])

print("Product of the elements in the given array: ",x.prod())
				
			

Output :

				
					Product of the elements in the given array:  15
				
			

check whether an array is empty.

convert an array into a CSV file.

Program to check whether the NumPy array is empty

In this python numpy program, we will check whether the NumPy array is empty.

Steps to solve the program
  1. Import the numpy library as np.
  2. Create an array using np.array().
  3. Get the size of the array (i.e. total number of elements) using size.
  4. Determine whether the given array is empty or not using an if-else statement.
  5. If the size of the array is 0 then the array is empty else the array is not empty.
  6. Print the respective output.
				
					import numpy as np
x = np.array([4,8,0])

if x.size == 0:
    print("Array is empty")
else:
    print("Array is not empty")
				
			

Output :

				
					Array is not empty
				
			

count the frequency of unique values.

calculate the product of an array.

Get the count the frequency of unique values from the NumPy array

In this python numpy program, we will count the frequency of unique values from the NumPy array.

Steps to solve the program
  1. Import the numpy library as np.
  2. Create an array using np.array().
  3. Get the unique values and their frequency from the array using np.unique(array,return_counts=True).
  4. Print the output.
				
					import numpy as np
x = np.array([8, 6, 7, 0, 7, 8, 6, 6])

unique, count = np.unique(x, return_counts=True)
print("Frequency of unique values in the given array:")
print(np.asarray((unique, count)))
				
			

Output :

				
					Frequency of unique values in the given array:
[[0 6 7 8]
 [1 3 2 2]]
				
			

get the magnitude of a vector.

check whether an array is empty.