
- 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
Python Pandas – Propagate non-null values backward
Use the “method” parameter of the fillna() method. For backward fill, use the value ‘bfill’ as shown below −
fillna(method='bfill')
Let’s say the following is our CSV file opened in Microsoft Excel with some NaN values −
At first, import the required library −
import pandas as pd
Load data from a CSV file into a Pandas DataFrame −
dataFrame = pd.read_csv("C:\Users\amit_\Desktop\SalesData.csv")
Example
Following is the code −
import pandas as pd # Load data from a CSV file into a Pandas DataFrame dataFrame = pd.read_csv("C:\Users\amit_\Desktop\SalesData.csv") print("DataFrame...\n",dataFrame) # propagate non null values backward res = dataFrame.fillna(method='bfill') print("\nDataFrame after backward fill...\n",res)
Output
This will produce the following output −
DataFrame... Car Reg_Price Units 0 BMW 2500 100.0 1 Lexus 3500 NaN 2 Audi 2500 120.0 3 Jaguar 2000 NaN 4 Mustang 2500 110.0 DataFrame after backward fill... Car Reg_Price Units 0 BMW 2500 100.0 1 Lexus 3500 120.0 2 Audi 2500 120.0 3 Jaguar 2000 110.0 4 Mustang 2500 110.0
- Related Articles
- Python Pandas – Propagate non-null values forward
- Python Pandas – Check for Null values using notnull()
- Python Pandas CustomBusinessHour - Roll provided date backward
- Python - Remove a column with all null values in Pandas
- How does COALESCE order results with NULL and NON-NULL values?
- Looping in JavaScript to count non-null and non-empty values
- Fetch maximum value from multiple columns with null and non-null values?
- Perform mathematical calculations in a MySQL table with NULL and NON-NULL values
- Display first non-null values with coalesce() in MySQL?
- Return only the non-empty and non-null values from a table and fill the empty and NULL values with the corresponding column values in MySQL?
- Update all the fields in a table with null or non-null values with MySQL
- How does the pandas series.equals() method handle the null values?
- Backward iteration in Python
- Query non-empty values of a row first in ascending order and then display NULL values
- Backward iteration in Python program

Advertisements