
- 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 Python code to fill all the missing values in a given dataframe
Solution
To solve this, we will follow the steps given below −
Define a dataframe
Apply df.interpolate funtion inside method =’linear’, limit_direction =’forward’ and fill NaN limit = 2
df.interpolate(method ='linear', limit_direction ='forward', limit = 2
Example
import pandas as pd df = pd.DataFrame({"Id":[1, 2, 3, None, 5], "Age":[12, 12, 14, 13, None], "Mark":[80, 90, None, 95, 85], }) print("Dataframe is:\n",df) print("Interpolate missing values:") print(df.interpolate(method ='linear', limit_direction ='forward', limit = 2))
Output
Dataframe is: Id Age Mark 0 1.0 12.0 80.0 1 2.0 12.0 90.0 2 3.0 14.0 NaN 3 NaN 13.0 95.0 4 5.0 NaN 85.0 Interpolate missing values: Id Age Mark 0 1.0 12.0 80.0 1 2.0 12.0 90.0 2 3.0 14.0 92.5 3 4.0 13.0 95.0 4 5.0 13.0 85.0
- Related Articles
- Write a Python code to rename the given axis in a dataframe
- Write a Python code to filter palindrome names in a given dataframe
- Write a program in Python to find which column has the minimum number of missing values in a given dataframe
- Write a Python code to swap last two rows in a given dataframe
- Write a Python code to select any one random row from a given DataFrame
- Write a Python code to find the second lowest value in each column in a given dataframe
- Write a Python code to combine two given series and convert it to a dataframe
- Python - Remove the missing (NaN) values in the DataFrame
- Fill missing numeric values in a JavaScript array
- Write a Python program to reshape a given dataframe in different ways
- Write a program in Python to convert a given dataframe to a LaTex document
- Python Pandas - Fill missing columns values (NaN) with constant values
- 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
- How to fill a data.table row with missing values in R?

Advertisements