Tag Archives: python for data analyst

Pandas Example – Write a Pandas program to convert a NumPy array to a Pandas series

(Python Example for Beginners)   Write a Pandas program to convert a NumPy array to a Pandas series. Sample NumPy array: d1 = [10, 20, 30, 40, 50]   Sample Solution : Python Code : import numpy as np import pandas as pd np_array = np.array([10, 20, 30, 40, 50]) print(“NumPy array:”) print(np_array) new_series = pd.Series(np_array) …

Python Example – Write a Pandas program to create and display a one-dimensional array-like object containing an array of data

(Python Example for Beginners)   Write a Pandas program to create and display a one-dimensional array-like object containing an array of data.   Sample Solution : Python Code : import pandas as pd ds = pd.Series([2, 4, 6, 8, 10]) print(ds) Sample Output: 0 2 1 4 2 6 3 8 4 10 dtype: int64   …

Python Example – Write a NumPy program to remove the trailing whitespaces of all the elements of a given array

(Python Example for Beginners)   Write a NumPy program to remove the trailing whitespaces of all the elements of a given array.   Sample Solution: Python Code: import numpy as np x = np.array([‘ python exercises ‘, ‘ PHP ‘, ‘ java ‘, ‘ C++’], dtype=np.str) print(“Original Array:”) print(x) rstripped_char = np.char.rstrip(x) print(“nRemove the trailing …

Python Example – Write a NumPy program to remove the leading whitespaces of all the elements of a given array

(Python Example for Beginners)   Write a NumPy program to remove the leading whitespaces of all the elements of a given array.   Sample Solution: Python Code: import numpy as np x = np.array([‘ python exercises ‘, ‘ PHP ‘, ‘ java ‘, ‘ C++’], dtype=np.str) print(“Original Array:”) print(x) lstripped_char = np.char.lstrip(x) print(“nRemove the leading …

Python Example – Write a NumPy program to repeat all the elements three times of a given array of string

(Python Example for Beginners)   Write a NumPy program to repeat all the elements three times of a given array of string.   Sample Solution: Python Code: import numpy as np x1 = np.array([‘Python’, ‘PHP’, ‘Java’, ‘C++’], dtype=np.str) print(“Original Array:”) print(x1) new_array = np.char.multiply(x1, 3) print(“New array:”) print(new_array) Sample Input: ([‘Python’, ‘PHP’, ‘Java’, ‘C++’], dtype=np.str) …

Python Example – Write a NumPy program to compute the histogram of nums against the bins

(Python Example for Beginners)   Write a NumPy program to compute the histogram of nums against the bins.   Sample Solution: Python Code: import numpy as np import matplotlib.pyplot as plt nums = np.array([0.5, 0.7, 1.0, 1.2, 1.3, 2.1]) bins = np.array([0, 1, 2, 3]) print(“nums: “,nums) print(“bins: “,bins) print(“Result:”, np.histogram(nums, bins)) plt.hist(nums, bins=bins) plt.show() …

Python Example – Write a NumPy program to compute the 80th percentile for all elements in a given array along the second axis

(Python Example for Beginners)   Write a NumPy program to compute the 80th percentile for all elements in a given array along the second axis.   Sample Solution: Python Code: import numpy as np x = np.arange(12).reshape((2, 6)) print(“nOriginal array:”) print(x) r1 = np.percentile(x, 80, 1) print(“n80th percentile for all elements of the said array along …

Python Example – Write a NumPy program to create a random vector of size 10 and sort it

(Python Example for Beginners)   Write a NumPy program to create a random vector of size 10 and sort it.   Sample Solution: Python Code : import numpy as np x = np.random.random(10) print(“Original array:”) print(x) x.sort() print(“Sorted array:”) print(x) Sample Output: Original array: [ 0.73123073 0.67714015 0.95615347 0.4759837 0.88789818 0.69104042 0.59996415 0.26144489 0.51618644 0.89943882] …

Python Example – Write a NumPy program to calculate the QR decomposition of a given matrix

(Python Example for Beginners)   Write a NumPy program to calculate the QR decomposition of a given matrix. From Wikipedia: In linear algebra, a QR decomposition (also called a QR factorization) of a matrix is a decomposition of a matrix A into a product A = QR of an orthogonal matrix Q and an upper …

Python Example – Write a NumPy program to compute the inverse of a given matrix

(Python Example for Beginners)   Write a NumPy program to compute the inverse of a given matrix.   Sample Solution : Python Code : import numpy as np m = np.array([[1,2],[3,4]]) print(“Original matrix:”) print(m) result = np.linalg.inv(m) print(“Inverse of the said matrix:”) print(result) Sample Output: Original matrix: [[1 2] [3 4]] Inverse of the said matrix: …