Day: April 3, 2021

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

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