- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Write a Python program to count the total number of ages between 20 to 30 in a DataFrame
Input −
Assume, you have a DataFrame,
Id Age 0 1 21 1 2 23 2 3 32 3 4 35 4 5 18
Output −
Total number of age between 20 to 30 is 2.
Solution
To solve this, we will follow the below approaches.
Define a DataFrame
Set the DataFrame Age column between 20,30. Store it in result DataFrame. It is defined below,
df[df['Age'].between(20,30)]
Finally, calculate the length of the result.
Example
Let us see the following implementation to get a better understanding.
import pandas as pd data = {'Id':[1,2,3,4,5],'Age':[21,23,32,35,18]} df = pd.DataFrame(data) print(df) print("Count the age between 20 to 30") result = df[df['Age'].between(20,30)] print(len(result))
Output
Id Age 0 1 21 1 2 23 2 3 32 3 4 35 4 5 18 Count the age between 20 to 30 2
- Related Articles
- Write a program in Python to count the total number of leap years in a given DataFrame
- Write a python program to count total bits in a number?
- Write a program in Python to count the total number of integer, float and object data types in a given series
- Write a program in Python to count the records based on the designation in a given DataFrame
- Write a program in Python to count the number of digits in a given number N
- C# program to count total bits in a number
- Java program to count total bits in a number
- C# program to count total set bits in a number
- Write a Python program to quantify the shape of a distribution in a dataframe
- Write a program in Python to covert the datatype of a particular column in a dataframe
- Write a program in Python to find which column has the minimum number of missing values in a given dataframe
- Write a program in Python to modify the diagonal of a given DataFrame by 1
- Write a Python program to export a dataframe to an html file
- Write a program in Python to convert a given dataframe to a LaTex document
- Write a Python program to reshape a given dataframe in different ways

Advertisements