Tag Archives: python for data analyst

Pandas Example – Write a Pandas program to select the rows where number of attempts in the examination is less than 2 and score greater than 15

(Python Example for Beginners)   Write a Pandas program to select the rows where number of attempts in the examination is less than 2 and score greater than 15. Sample DataFrame: Sample Python dictionary data and list labels: exam_data = {‘name’: [‘Anastasia’, ‘Dima’, ‘Katherine’, ‘James’, ‘Emily’, ‘Michael’, ‘Matthew’, ‘Laura’, ‘Kevin’, ‘Jonas’], ‘score’: [12.5, 9, 16.5, …

Pandas Example – Write a Pandas program to select the rows where the score is missing, i.e. is NaN

(Python Example for Beginners)   Write a Pandas program to select the rows where the score is missing, i.e. is NaN. Sample DataFrame: Sample Python dictionary data and list labels: exam_data = {‘name’: [‘Anastasia’, ‘Dima’, ‘Katherine’, ‘James’, ‘Emily’, ‘Michael’, ‘Matthew’, ‘Laura’, ‘Kevin’, ‘Jonas’], ‘score’: [12.5, 9, 16.5, np.nan, 9, 20, 14.5, np.nan, 8, 19], ‘attempts’: …

Pandas Example – Write a Pandas program to get the powers of an array values element-wise

(Python Example for Beginners)   Write a Pandas program to get the powers of an array values element-wise. Note: First array elements raised to powers from second array Sample data: {‘X’:[78,85,96,80,86], ‘Y’:[84,94,89,83,86],’Z’:[86,97,96,72,83]}   Sample Solution : Python Code : import pandas as pd df = pd.DataFrame({‘X’:[78,85,96,80,86], ‘Y’:[84,94,89,83,86],’Z’:[86,97,96,72,83]}); print(df) Sample Output: X Y Z 0 78 84 …

Pandas Example – Write a Pandas program to check the equality of two given series

(Python Example for Beginners)   Write a Pandas program to check the equality of two given series.   Sample Solution : Python Code : import pandas as pd nums1 = pd.Series([1, 8, 7, 5, 6, 5, 3, 4, 7, 1]) nums2 = pd.Series([1, 8, 7, 5, 6, 5, 3, 4, 7, 1]) print(“Original Series:”) print(nums1) print(nums2) …

Pandas Example – Write a Pandas program to create a TimeSeries to display all the Sundays of given year.

(Python Example for Beginners)   Write a Pandas program to create a TimeSeries to display all the Sundays of given year.   Sample Solution : Python Code : import pandas as pd result = pd.Series(pd.date_range(‘2020-01-01′, periods=52, freq=’W-SUN’)) print(“All Sundays of 2019:”) print(result) Sample Output: All Sundays of 2019: 0 2020-01-05 1 2020-01-12 2 2020-01-19 3 2020-01-26 …

Pandas Example – Write a Pandas program to compute difference of differences between consecutive numbers of a given series

(Python Example for Beginners)   Write a Pandas program to compute difference of differences between consecutive numbers of a given series.   Sample Solution : Python Code : import pandas as pd series1 = pd.Series([1, 3, 5, 8, 10, 11, 15]) print(“Original Series:”) print(series1) print(“nDifference of differences between consecutive numbers of the said series:”) print(series1.diff().tolist()) print(series1.diff().diff().tolist()) …

Pandas Example – Write a Pandas program to calculate the number of characters in each word in a given series

(Python Example for Beginners)   Write a Pandas program to calculate the number of characters in each word in a given series.   Sample Solution : Python Code : import pandas as pd series1 = pd.Series([‘Php’, ‘Python’, ‘Java’, ‘C#’]) print(“Original Series:”) print(series1) result = series1.map(lambda x: len(x)) print(“nNumber of characters in each word in the said …

Pandas Example – Write a Pandas program to calculate the frequency counts of each unique value of a given series

(Python Example for Beginners)   Write a Pandas program to calculate the frequency counts of each unique value of a given series.   Sample Solution : Python Code : import pandas as pd import numpy as np num_series = pd.Series(np.take(list(‘0123456789’), np.random.randint(10, size=40))) print(“Original Series:”) print(num_series) print(“Frequency of each unique value of the said series.”) result = …

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 …