Challenge: Filling Null Values
To handle null values while retaining each row of the DataFrame, we can utilize the fillna()
method. This allows us to populate each empty cell with a specific value (like a string or number) rather than eliminating it.
To replace null values with the number 0, the fillna()
method is used:
python9123import pandas as pddf = pd.read_csv(file.csv)new_df = df.fillna(0)
Task
Swipe to start coding
You are given a DataFrame
named wine_data
.
- Replace null values with the string
'no'
.
Solution
9
1
2
3
4
5
6
7
8
9
import pandas as pd
wine_data = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/a43d24b6-df61-4e11-9c90-5b36552b3437/wine_with_nan.csv')
# Write your code below
wine_data = wine_data.fillna('no')
# Testing the result
print(wine_data.info())
Everything was clear?
Thanks for your feedback!
Section 3. Chapter 9
9
1
2
3
4
5
6
7
8
9
import pandas as pd
wine_data = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/a43d24b6-df61-4e11-9c90-5b36552b3437/wine_with_nan.csv')
# Write your code below
wine_data = ___
# Testing the result
print(wine_data.info())
Ask AI
Ask anything or try one of the suggested questions to begin our chat