

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python Pandas – Propagate non-null values forward
Use the “method” parameter of the fillna() method. For forward fill, use the value ‘ffill’ as shown below −
fillna(method='ffill')
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 complete 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 forward res = dataFrame.fillna(method='ffill') print("\nDataFrame after forward 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 forward fill... Car Reg_Price Units 0 BMW 2500 100.0 1 Lexus 3500 100.0 2 Audi 2500 120.0 3 Jaguar 2000 120.0 4 Mustang 2500 110.0
- Related Questions & Answers
- Python Pandas – Propagate non-null values backward
- Python Pandas – Check for Null values using notnull()
- Looping in JavaScript to count non-null and non-empty values
- How does COALESCE order results with NULL and NON-NULL values?
- Python - Remove a column with all null values in Pandas
- Fetch maximum value from multiple columns with null and non-null values?
- Display first non-null values with coalesce() in MySQL?
- Perform mathematical calculations in a MySQL table with NULL and NON-NULL values
- 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?
- Query non-empty values of a row first in ascending order and then display NULL values
- Python Pandas - Indicate duplicate index values
- How to check for 'undefined' or 'null' in a JavaScript array and display only non-null values?
- Can MySQL INT type be non-zero NULL?
Advertisements