Python for Data Analyst

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 check two random arrays are equal or not

(Python Example for Beginners)   Write a NumPy program to check two random arrays are equal or not.   Sample Solution: Python Code : import numpy as np x = np.random.randint(0,2,6) print(“First array:”) print(x) y = np.random.randint(0,2,6) print(“Second array:”) print(y) print(“Test above two arrays are equal or not!”) array_equal = np.allclose(x, y) print(array_equal) Sample Output: …

Python Example – Write a NumPy program to generate six random integers between 10 and 30

(Python Example for Beginners)   Write a NumPy program to generate six random integers between 10 and 30.   Sample Solution: Python Code: import numpy as np x = np.random.randint(low=10, high=30, size=6) print(x) Sample Output: [14 25 20 12 27 22]     Python Example – Write a NumPy program to generate six random integers …

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

(Python Example for Beginners)   Write a NumPy program to compute the condition number of a given matrix. From Wikipedia: In the field of numerical analysis, the condition number of a function with respect to an argument measures how much the output value of the function can change for a small change in the input …

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 …