Python for Business Analyst

Python Example – Write a Pandas program to convert a Panda module Series to Python list and it’s type

(Python Example for Beginners)   Write a Pandas program to convert a Panda module Series to Python list and it’s type.   Sample Solution : Python Code : import pandas as pd ds = pd.Series([2, 4, 6, 8, 10]) print(“Pandas Series and type”) print(ds) print(type(ds)) print(“Convert Pandas Series to Python list”) print(ds.tolist()) print(type(ds.tolist())) Sample Output: Pandas …

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 pearson product-moment correlation coefficients of two given arrays

(Python Example for Beginners)   Write a NumPy program to compute pearson product-moment correlation coefficients of two given arrays.   Sample Solution: Python Code: import numpy as np x = np.array([0, 1, 3]) y = np.array([2, 4, 5]) print(“nOriginal array1:”) print(x) print(“nOriginal array1:”) print(y) print(“nPearson product-moment correlation coefficients of the said arrays:n”,np.corrcoef(x, y)) Sample Output: …

Python Example – Write a NumPy program to compute the covariance matrix of two given arrays

(Python Example for Beginners)   Write a NumPy program to compute the covariance matrix of two given arrays.   Sample Solution: Python Code: import numpy as np x = np.array([0, 1, 2]) y = np.array([2, 1, 0]) print(“nOriginal array1:”) print(x) print(“nOriginal array1:”) print(y) print(“nCovariance matrix of the said arrays:n”,np.cov(x, y)) Sample Output: Original array1: [0 …

Python Example – Write a NumPy program to compute the weighted of a given array

(Python Example for Beginners)   Write a NumPy program to compute the weighted of a given array.   Sample Solution: Python Code: import numpy as np x = np.arange(5) print(“nOriginal array:”) print(x) weights = np.arange(1, 6) r1 = np.average(x, weights=weights) r2 = (x*(weights/weights.sum())).sum() assert np.allclose(r1, r2) print(“nWeighted average of the said array:”) print(r1) Sample Output: …

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 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 shuffle numbers between 0 and 10 (inclusive)

(Python Example for Beginners)   Write a NumPy program to shuffle numbers between 0 and 10 (inclusive).   Sample Solution: Python Code : import numpy as np x = np.arange(10) np.random.shuffle(x) print(x) print(“Same result using permutation():”) print(np.random.permutation(10)) Sample Output: [2 7 1 5 3 9 0 4 6 8] Same result using permutation(): [8 9 …