<jupyter_start><jupyter_text>**Examples**<jupyter_code>import numpy as np
import pandas as pd
df = pd.DataFrame([[np.nan, 3, np.nan, 0],
                   [4, 5, np.nan, 2],
                   [np.nan, np.nan, np.nan, 6],
                   [np.nan, 4, np.nan, 5]],
                  columns=list('PQRS'))
df<jupyter_output><empty_output><jupyter_text>Replace all NaN elements with 0s:<jupyter_code>df.fillna(0)<jupyter_output><empty_output><jupyter_text>We can also propagate non-null values forward or backward:<jupyter_code>df.fillna(method='ffill')<jupyter_output><empty_output><jupyter_text>Replace all NaN elements in column ‘P’, ‘Q’, ‘R’, and ‘S’, with 0, 2, 3, and 4 respectively:<jupyter_code>values = {'P': 0, 'Q': 2, 'R': 3, 'S': 4}
df.fillna(value=values)<jupyter_output><empty_output><jupyter_text>Only replace the first NaN element:<jupyter_code>df.fillna(value=values, limit=1)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1581798483206934, 'fraction_numerical': 0.020585048754062838, 'mean_word_length': 4.668711656441718, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '353993', 'license': 'MIT', 'n_tokens_mistral': 379, 'n_tokens_neox': 348, 'n_words': 72, 'path': 'Data Science and Machine Learning/Machine-Learning-In-Python-THOROUGH/EXAMPLES/DIFFERENT/W3RESOURCE/PANDAS_DATAFRAME_FILLNA.ipynb', 'repo_name': 'okara83/Becoming-a-Data-Scientist', 'size': '923'}