Tag Archives: python for beginners

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 count the number of rows and columns of a DataFrame

(Python Example for Beginners)   Write a Pandas program to count the number of rows and columns of a DataFrame. Sample DataFrame: 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’: [1, 3, 2, 3, 2, 3, 1, 1, …

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

(Python Example for Beginners)   Write a Pandas program to select the rows where the number of attempts in the examination is greater than 2. Sample DataFrame: 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’: [1, 3, 2, …

Pandas Example – Write a Pandas program to select the ‘name’ and ‘score’ columns from the following DataFrame

(Python Example for Beginners) Write a Pandas program to select the ‘name’ and ‘score’ columns from the following DataFrame. Sample DataFrame: 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’: [1, 3, 2, 3, 2, 3, 1, 1, 2, …

Pandas Example – Write a Pandas program to display a summary of the basic information about a specified DataFrame and its data

(Python Example for Beginners)   Write a Pandas program to display a summary of the basic information about a specified DataFrame and its data. Sample DataFrame: 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’: [1, 3, 2, 3, …

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 convert given series into a dataframe with its index as another column on the dataframe

(Python Example for Beginners)   Write a Pandas program to convert given series into a dataframe with its index as another column on the dataframe.   Sample Solution : Python Code : import numpy as np import pandas as pd char_list = list(‘ABCDEFGHIJKLMNOP’) num_arra = np.arange(8) num_dict = dict(zip(char_list, num_arra)) num_ser = pd.Series(num_dict) df = num_ser.to_frame().reset_index() …

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 the Euclidean distance between two given series

(Python Example for Beginners)   Write a Pandas program to compute the Euclidean distance between two given series.   Euclidean distance From Wikipedia, In mathematics, the Euclidean distance or Euclidean metric is the “ordinary” straight-line distance between two points in Euclidean space. With this distance, Euclidean space becomes a metric space. The associated norm is called …

Pandas Example – Write a Pandas program to filter words from a given series that contain atleast two vowels

(Python Example for Beginners)   Write a Pandas program to filter words from a given series that contain atleast two vowels.   Sample Solution : Python Code : import pandas as pd from collections import Counter color_series = pd.Series([‘Red’, ‘Green’, ‘Orange’, ‘Pink’, ‘Yellow’, ‘White’]) print(“Original Series:”) print(color_series) print(“nFiltered words:”) result = mask = color_series.map(lambda c: sum([Counter(c.lower()).get(i, …