search
Search
Publish
menu
menu search toc more_vert
Robocat
Guest 0reps
Thanks for the thanks!
close
Comments
Log in or sign up
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
help Ask a question
Share on Twitter
search
keyboard_voice
close
Searching Tips
Search for a recipe: "Creating a table in MySQL"
Search for an API documentation: "@append"
Search for code: "!dataframe"
Apply a tag filter: "#python"
Useful Shortcuts
/ to open search panel
Esc to close search panel
to navigate between search results
d to clear all current filters
Enter to expand content preview
icon_star
Doc Search
icon_star
Code Search Beta
SORRY NOTHING FOUND!
mic
Start speaking...
Voice search is only supported in Safari and Chrome.
Navigate to
A
A
share
thumb_up_alt
bookmark
arrow_backShare
Twitter
Facebook

Replacing missing values in Pandas DataFrame

Programming
chevron_right
Python
chevron_right
Pandas
chevron_right
Cookbooks
chevron_right
DataFrame Cookbooks
chevron_right
Handling Missing Values
schedule Jul 1, 2022
Last updated
local_offer PythonPandas
Tags

To replace missing values (NaN) in Pandas DataFrame, use the fillna(~) method.

Replacing missing value with a specific value

Consider the following DataFrame:

df = pd.DataFrame({"A":[3,pd.np.nan],"B":[pd.np.nan,6]})
df
A B
0 3.0 NaN
1 NaN 6.0

To fill all missing values with the value 8:

df.fillna(8)
A B
0 3.0 8.0
1 8.0 6.0

Replacing missing values in certain columns

Consider the same df as above:

df = pd.DataFrame({"A":[3,pd.np.nan],"B":[pd.np.nan,6]})
df
A B
0 3.0 NaN
1 NaN 6.0

To fill missing values in certain columns:

df.fillna({"A":8})
A B
0 3.0 NaN
1 8.0 6.0

Here, we are replacing all missing values in column A with 8.

Replacing missing values with value in preceding row

Consider the following DataFrame:

df = pd.DataFrame({"A":[3,pd.np.nan,5],"B":[pd.np.nan,6,7],"C":[8,pd.np.nan,pd.np.nan]})
df
A B
0 3.0 NaN
1 NaN 6.0
2 5.0 7.0

To fill missing values with value in the preceding row:

df.fillna(method="ffill")
A B C
0 3.0 NaN 8.0
1 3.0 6.0 8.0
2 5.0 7.0 8.0

Here, note the following:

  • method="ffill" standard for forward-fill, that is, we replace a missing value with the previous non-NaN value in the same column.

  • we still have NaN in column B because there is no previous row.

Replacing missing values with value in next row

Consider the following DataFrame:

df = pd.DataFrame({"A":[3,pd.np.nan,5],"B":[6,7,pd.np.nan],"C":[pd.np.nan,pd.np.nan,8]})
df
A B C
0 3.0 6.0 NaN
1 NaN 7.0 NaN
2 5.0 NaN 8.0

To fill missing values with value in the next row:

df.fillna(method="bfill")
A B C
0 3.0 6.0 8.0
1 5.0 7.0 8.0
2 5.0 NaN 8.0

Here, note the following:

  • method="bfill" standard for backward-fill, that is, we replace a missing value with the next non-NaN value in the same column.

  • we still have NaN in column B because there is no next row.

robocat
Published by Isshin Inada
Edited by 0 others
Did you find this page useful?
thumb_up
thumb_down
Ask a question or leave a feedback...