Pandas Articles

Page 24 of 42

Write a program in Python to calculate the default float quantile value for all the element in a Series

Vani Nalliappan
Vani Nalliappan
Updated on 25-Mar-2026 160 Views

A quantile represents the value below which a certain percentage of data falls. In pandas, the quantile() method calculates quantile values for a Series, with 0.5 being the default (median). Understanding Quantiles The quantile value of 0.5 represents the median − the middle value when data is sorted. For a Series with values [10, 20, 30, 40, 50], the 0.5 quantile (median) is 30.0. Syntax Series.quantile(q=0.5, interpolation='linear') Parameters: q − Float between 0 and 1 (default is 0.5) interpolation − Method to use when quantile lies between two data points ...

Read More

Write a program in Python to count the records based on the designation in a given DataFrame

Vani Nalliappan
Vani Nalliappan
Updated on 25-Mar-2026 184 Views

To count records based on designation in a pandas DataFrame, we use the groupby() method combined with count(). This groups rows by designation and counts occurrences in each group. Creating the DataFrame Let's start by creating a sample DataFrame with employee data ? import pandas as pd data = { 'Id': [1, 2, 3, 4, 5], 'Designation': ['architect', 'scientist', 'programmer', 'scientist', 'programmer'] } df = pd.DataFrame(data) print("DataFrame is:") print(df) DataFrame is: Id Designation 0 1 architect 1 ...

Read More

Write a program in Python to store the city and state names that start with 'k' in a given DataFrame into a new CSV file

Vani Nalliappan
Vani Nalliappan
Updated on 25-Mar-2026 734 Views

When working with pandas DataFrames, you often need to filter data based on specific criteria and save the results to a file. This example demonstrates how to filter cities and states that start with 'K' and export them to a CSV file. Problem Statement Given a DataFrame with City and State columns, we need to find rows where both the city name and state name start with 'K', then save these filtered results to a new CSV file. Solution Approach To solve this problem, we will follow these steps: Create a DataFrame with city ...

Read More

Write a Python code to select any one random row from a given DataFrame

Vani Nalliappan
Vani Nalliappan
Updated on 25-Mar-2026 485 Views

Sometimes you need to select a random row from a Pandas DataFrame for sampling or testing purposes. Python provides several approaches to accomplish this task using iloc with random index generation or the sample() method. Sample DataFrame Let's start with a sample DataFrame to demonstrate the methods ? import pandas as pd data = {'Id': [1, 2, 3, 4, 5], 'Name': ['Adam', 'Michael', 'David', 'Jack', 'Peter']} df = pd.DataFrame(data) print("DataFrame is") print(df) DataFrame is Id Name 0 1 Adam ...

Read More

Write a Python program to sort a given DataFrame by name column in descending order

Vani Nalliappan
Vani Nalliappan
Updated on 25-Mar-2026 692 Views

Sorting a DataFrame by a specific column is a common task in data analysis. Pandas provides the sort_values() method to sort DataFrames by one or more columns in ascending or descending order. Input DataFrame Let's start with a sample DataFrame containing Id and Name columns ? Id Name 0 1 Adam 1 2 Michael 2 3 David 3 4 Jack 4 5 Peter ...

Read More

Write a Python function which accepts DataFrame Age, Salary columns second, third and fourth rows as input and find the mean, product of values

Vani Nalliappan
Vani Nalliappan
Updated on 25-Mar-2026 661 Views

This tutorial shows how to write a Python function that accepts a DataFrame and calculates the mean and product of specific rows from Age and Salary columns using iloc for row slicing. Sample DataFrame Let's start with a sample DataFrame containing employee data ? import pandas as pd data = [[1, 27, 40000], [2, 22, 25000], [3, 25, 40000], [4, 23, 35000], [5, 24, 30000], [6, 32, 30000], [7, 30, 50000], [8, 28, 20000], [9, 29, 32000], [10, 27, 23000]] df = pd.DataFrame(data, columns=('Id', 'Age', 'salary')) print(df) ...

Read More

Write a Python program to count the total number of ages between 20 to 30 in a DataFrame

Vani Nalliappan
Vani Nalliappan
Updated on 25-Mar-2026 1K+ Views

When working with Pandas DataFrames, you often need to count rows that meet specific conditions. In this case, we'll count how many ages fall between 20 and 30 using the between() method. Sample DataFrame Let's start with a DataFrame containing ID and Age columns ? import pandas as pd data = {'Id': [1, 2, 3, 4, 5], 'Age': [21, 23, 32, 35, 18]} df = pd.DataFrame(data) print(df) Id Age 0 1 21 1 2 23 2 3 ...

Read More

Write a program in Python to print the 'A' grade students' names from a DataFrame

Vani Nalliappan
Vani Nalliappan
Updated on 25-Mar-2026 697 Views

Sometimes we need to filter DataFrame records based on specific column values. In this example, we'll filter a student DataFrame to show only students with 'A' grades and display their names. Sample DataFrame Let's start by creating a DataFrame with student information ? import pandas as pd data = [[1, 'stud1', 'A'], [2, 'stud2', 'B'], [3, 'stud3', 'C'], [4, 'stud4', 'A'], [5, 'stud5', 'A']] df = pd.DataFrame(data, columns=('Id', 'Name', 'Grade')) print("DataFrame is", df) DataFrame is Id Name Grade 0 1 stud1 A 1 ...

Read More

Write a program in Python to find the minimum age of an employee id and salary in a given DataFrame

Vani Nalliappan
Vani Nalliappan
Updated on 25-Mar-2026 1K+ Views

Finding the employee with the minimum age in a DataFrame is a common data analysis task. We can use pandas boolean indexing to filter rows where the age equals the minimum age value. Problem Statement Given a DataFrame with employee data (Id, Age, Salary), we need to find the Id and Salary of the employee with the minimum age. Input DataFrame Id Age Salary 0 1 27 40000 1 2 22 25000 2 3 ...

Read More

Write a program in Python to find the maximum length of a string in a given Series

Vani Nalliappan
Vani Nalliappan
Updated on 25-Mar-2026 616 Views

In this article, we'll learn how to find the string with the maximum length in a Pandas Series. We'll explore multiple approaches including a manual loop method and built-in Pandas functions. Problem Statement Given a Pandas Series containing strings like ["one", "two", "eleven", "pomegranates", "three"], we need to find the string with the maximum length. In this case, "pomegranates" has 12 characters, making it the longest string. Method 1: Using Manual Loop The basic approach involves iterating through the Series and tracking the longest string ? import pandas as pd # Create a ...

Read More
Showing 231–240 of 418 articles
« Prev 1 22 23 24 25 26 42 Next »
Advertisements