0
0
Pandasdata~15 mins

Forward fill and backward fill in Pandas - Mini Project: Build & Apply

Choose your learning style9 modes available
Forward fill and backward fill
📖 Scenario: Imagine you have a small table of daily temperatures, but some days are missing values. You want to fill in those missing days by using the last known temperature or the next known temperature.
🎯 Goal: You will create a pandas DataFrame with some missing temperature values. Then, you will add variables to control filling methods. Next, you will apply forward fill and backward fill to fill missing values. Finally, you will print the filled DataFrames.
📋 What You'll Learn
Create a pandas DataFrame called temps with dates as index and temperature values including NaN
Create a variable called fill_method to choose between forward fill and backward fill
Use the fillna() method with method=fill_method to fill missing values
Print the filled DataFrame
💡 Why This Matters
🌍 Real World
Data often has missing values. Filling missing data helps keep analysis accurate and complete, like filling missing temperature readings in weather data.
💼 Career
Data scientists and analysts frequently clean data by filling missing values to prepare datasets for modeling and reporting.
Progress0 / 4 steps
1
Create the temperature DataFrame with missing values
Import pandas as pd. Create a DataFrame called temps with the dates '2024-06-01' to '2024-06-05' as the index. The temperature values should be 20.5, NaN, 22.0, NaN, and 23.5 respectively.
Pandas
Need a hint?

Use pd.date_range to create the dates index. Use np.nan for missing values.

2
Create a variable to select fill method
Create a variable called fill_method and set it to the string 'ffill' to represent forward fill.
Pandas
Need a hint?

Set fill_method exactly to the string 'ffill'.

3
Apply forward fill or backward fill to fill missing values
Create a new DataFrame called filled_temps by applying temps.fillna(method=fill_method) to fill missing values using the method stored in fill_method.
Pandas
Need a hint?

Use the fillna() method with the method parameter set to fill_method.

4
Print the filled DataFrame
Print the filled_temps DataFrame to see the result of forward fill.
Pandas
Need a hint?

Use print(filled_temps) to show the DataFrame.