Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles by Vani Nalliappan
Page 8 of 13
Write a Python program to count the total number of ages between 20 to 30 in a DataFrame
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 MoreWrite a program in Python to print the 'A' grade students' names from a DataFrame
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 MoreWrite a program in Python to find the minimum age of an employee id and salary in a given DataFrame
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 MoreWrite a program in Python to find the maximum length of a string in a given Series
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 MoreWrite a Python program to find the maximum value from first four rows in a given series
A Pandas Series is a one-dimensional data structure that allows you to store and manipulate data efficiently. Finding the maximum value from specific rows is a common operation when analyzing data. Problem Statement Input − Assume you have a Series: 0 11 1 12 2 66 3 24 4 80 5 40 6 28 7 50 Output − Maximum value from first four rows is 66. Solution To ...
Read MoreWrite a program in Python to round all the elements in a given series
Rounding elements in a Pandas Series is a common data preprocessing task. Python provides multiple approaches: using the built-in round() method, manual iteration, or NumPy functions. Sample Data Let's start with a Series containing decimal values ? import pandas as pd data = pd.Series([1.3, 2.6, 3.9, 4.8, 5.6]) print("Original Series:") print(data) Original Series: 0 1.3 1 2.6 2 3.9 3 4.8 4 5.6 dtype: float64 Using round() Method The most efficient approach is using ...
Read MoreHow to print the days in a given month using Python?
To print the days in a given month using Python, we can use Pandas to work with date series and extract the number of days using the dt.daysinmonth attribute. This is particularly useful when working with date datasets and you need to know how many days are in specific months. Solution To solve this, we will follow the steps given below ? Import pandas library Create a date range using pd.date_range() Convert to pandas Series Use Series.dt.daysinmonth to find the number of days Example Let us see the complete implementation to get a ...
Read MoreWrite a program in Python to print the day of the year in a given date series
When working with date data in Pandas, you can extract the day of the year (1-366) from a date series using the dt.dayofyear accessor. This is useful for analyzing seasonal patterns or calculating time differences. Creating a Date Series First, let's create a date series using pd.date_range() to generate consecutive dates ? import pandas as pd # Create a date range starting from 2020-01-10 with 5 periods date_range = pd.date_range('2020-01-10', periods=5) date_series = pd.Series(date_range) print("Date Series:") print(date_series) Date Series: 0 2020-01-10 1 2020-01-11 2 2020-01-12 3 ...
Read MoreWrite a Python code to concatenate two Pandas series into a single series without repeating the index
When working with Pandas Series, you often need to combine two series into one. By default, concatenating series preserves original indices, which can create duplicates. Using ignore_index=True creates a new sequential index starting from 0. Syntax pd.concat([series1, series2], ignore_index=True) Creating Two Series Let's start by creating two sample series with sequential data ? import pandas as pd series_one = pd.Series([1, 2, 3]) series_two = pd.Series([4, 5, 6]) print("Series One:") print(series_one) print("Series Two:") print(series_two) Series One: 0 1 1 2 2 ...
Read MoreWrite a Python code to create a series with your range values, generate a new row as sum of all the values and then convert the series into json file
To create a Pandas series with range values, add a sum row, and convert to JSON format, we need to follow a structured approach using pandas library functions. Solution To solve this, we will follow the steps given below − Define a series with a range of 1 to 10 Find the sum of all the values Convert the series into JSON file format Let us see the following implementation to get a better understanding ? Example import pandas as pd ...
Read More