Day: April 3, 2021

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

Hits: 4 (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 < …

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

Hits: 4 (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 …

Pandas Example – Write a Pandas program to convert a given Series to an array

Hits: 8 (Python Example for Beginners)   Write a Pandas program to convert a given Series to an array.   Sample Solution : Python Code : import pandas as pd import numpy as np s1 = pd.Series([‘100’, ‘200’, ‘python’, ‘300.12’, ‘400’]) print(“Original Data Series:”) print(s1) print(“Series to an array”) a = np.array(s1.values.tolist()) print (a) Sample Output: …

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

Hits: 5 (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 …

Pandas Example – Write a Pandas program to compare the elements of the two Pandas Series

Hits: 5 (Python Example for Beginners)   Write a Pandas program to compare the elements of the two Pandas Series. Sample Series: [2, 4, 6, 8, 10], [1, 3, 5, 7, 10]   Sample Solution: Python Code : import pandas as pd ds1 = pd.Series([2, 4, 6, 8, 10]) ds2 = pd.Series([1, 3, 5, 7, …

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

Hits: 1 (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 …

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

Hits: 0 (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: …

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

Hits: 4 (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 …

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

Hits: 11 (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 …

Python Example – Write a NumPy program to capitalize the first letter, lowercase, uppercase, swapcase, title-case of all the elements of a given array

Hits: 7 (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 = …

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

Hits: 2 (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’, …

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

Hits: 1 (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, …

Python Example – Write a NumPy program to compute pearson product-moment correlation coefficients of two given arrays

Hits: 5 (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)) …