search
Search
Login
Unlock 100+ guides
menu
menu
web
search toc
close
Comments
Log in or sign up
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
What does this mean?
Why is this true?
Give me some examples!
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
check_circle
Mark as learned
thumb_up
0
thumb_down
0
chat_bubble_outline
0
Comment
auto_stories Bi-column layout
settings

Replacing missing values in Pandas DataFrame

schedule Aug 11, 2023
Last updated
local_offer
PythonPandas
Tags
mode_heat
Master the mathematics behind data science with 100+ top-tier guides
Start your free 7-days trial now!

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
Comment
Citation
Ask a question or leave a feedback...