Tag Archives: python for beginners

Pandas Example – Write a Pandas program to convert a series of date strings to a timeseries.

(Python Example for Beginners)   Write a Pandas program to convert a series of date strings to a timeseries.   Sample Solution : Python Code : import pandas as pd date_series = pd.Series([’01 Jan 2015′, ’10-02-2016′, ‘20180307’, ‘2014/05/06’, ‘2016-04-12’, ‘2019-04-06T11:20’]) print(“Original Series:”) print(date_series) print(“nSeries of date strings to a timeseries:”) print(pd.to_datetime(date_series)) Sample Output: Original Series: 0 …

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

(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: Original Data …

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

(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, 10]) print(“Series1:”) …

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