
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Write a program in Python to count the total number of leap years in a given DataFrame
Input −
Assume, DataFrame is,
DataFrame is year days 0 2002 365 1 2004 366 2 2012 366 3 2018 365 4 2020 366
Output −
``python Count the number of leap years are:- 3 ```
Solution
To solve this, we will follow the below approaches.
Define a DataFrame
Set the condition year%4==0 and days==366 to verify the DataFrame value. It is defined below,
df[(df['year']%4==0) & (df['days']==366)]
Finally, print the length of DataFrame.
Example
Let us see the following implementation to get a better understanding.
import pandas as pd data = { 'year':[2002,2004,2012,2018,2020], 'days': [365,366,366,365,366]} df = pd.DataFrame(data) df1 = df[(df['year']%4==0) & (df['days']==366)] print("Count is", len(df1))
Output
Count is 3
- Related Articles
- Write a Python program to count the total number of ages between 20 to 30 in a DataFrame
- Write a python program to count total bits in a number?
- How to get the total number of leap days in the years within range in Python?
- Write a program in Python to count the number of digits in a given number N
- 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 total number of integer, float and object data types in a given series
- Write a Python program to reshape a given dataframe in different ways
- Write a program in Python to modify the diagonal of a given DataFrame by 1
- 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 convert a given dataframe to a LaTex document
- Write a program in Python to transpose the index and columns in a given DataFrame
- Write a program in Python to localize Asian timezone for a given dataframe
- Write a program in Python to remove first duplicate rows in a given dataframe
- Program to count number of islands in a given matrix in Python
- Java program to count total bits in a number

Advertisements