Tag Archives: python for business analysts

Pandas Example – Write a Pandas program to create a subset of a given series based on value and condition

(Python Example for Beginners)   Write a Pandas program to create a subset of a given series based on value and condition.   Sample Solution : Python Code : import pandas as pd s = pd.Series([0, 1,2,3,4,5,6,7,8,9,10]) print(“Original Data Series:”) print(s) print(“nSubset of the above Data Series:”) n = 6 new_s = s[s < n] print(new_s) …

Pandas Example – Write a Pandas program to sort a given Series

(Python Example for Beginners)   Write a Pandas program to sort a given Series.   Sample Solution : Python Code : import pandas as pd s = pd.Series([‘100’, ‘200’, ‘python’, ‘300.12’, ‘400’]) print(“Original Data Series:”) print(s) new_s = pd.Series(s).sort_values() print(new_s) Sample Output: Original Data Series: 0 100 1 200 2 python 3 300.12 4 400 dtype: …

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 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 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 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 capitalize the first letter, lowercase, uppercase, swapcase, title-case of all the elements of a given array

(Python Example for Beginners)   Write a NumPy program to capitalize the first letter, lowercase, uppercase, swapcase, title-case of all the elements of a given array.   Sample Solution: Python Code: import numpy as np x = np.array([‘python’, ‘PHP’, ‘java’, ‘C++’], dtype=np.str) print(“Original Array:”) print(x) capitalized_case = np.char.capitalize(x) lowered_case = np.char.lower(x) uppered_case = np.char.upper(x) swapcased_case …

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 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: …