<jupyter_start><jupyter_text>Missing DataNaN is the default missing value marker for reasons of computational speed and convenience.  Pandas will be able to easily detect a NaN value with data of different types: floating point, integer, boolean, and general object.<jupyter_code>import numpy as np
np.random.seed(0)
import pandas as pd
print(1 + np.nan)
print(0 *  np.nan)
print(np.nan != np.nan)
print(np.nan == np.nan)
data = np.array([1, None, 3, 4])

print(data)
data = np.array([1, np.nan, 3, 4]) 

print(data)
print(np.sum(data))
print(np.nansum(data))
pd.Series([1, np.nan, 2, None])<jupyter_output><empty_output><jupyter_text>Pandas NaN Examples<jupyter_code>def highlight_nan(data: pd.DataFrame, color: str) -> pd.DataFrame:
    attr = f'background-color: {color}'
    is_nan = pd.isna(data)
    return pd.DataFrame(
        np.where(is_nan, attr, ''),
        index=data.index,
        columns=data.columns
    )

def df_info(df: pd.DataFrame) -> None:
    return df.style.apply(
        highlight_nan,
        color='darkorange',
        axis=None
    )
df = pd.DataFrame(
    np.random.randn(5, 3),
    index=["a", "c", "e", "f", "h"],
    columns=["one", "two", "three"],
)

df_info(df)
df["one"]["a"] = None
df["two"]["f"] = None

df_info(df)
df_info(pd.isna(df))
df_check = pd.isna(df)

df_check.any(axis=1)<jupyter_output><empty_output><jupyter_text>Remove NaN Values<jupyter_code>df_info(df.dropna())
df_info(df.dropna(axis='columns'))
df_info(df.dropna(axis='rows'))<jupyter_output><empty_output><jupyter_text>Operations with NaN Values- When summing data, NA (missing) values will be treated as zero.- If the data are all NA, the result will be 0.- Cumulative methods like cumsum() and cumprod() ignore NA values by default, but preserve them in the resulting arrays. To override this behaviour and include NA values, use skipna=False.<jupyter_code>df_info(df)
print(df["two"].sum())
print(df.sum())<jupyter_output>one      4.362618
two      2.238033
three    1.796378
dtype: float64<jupyter_text>Fill NaN values<jupyter_code>df_info(df.fillna(value=0.0))
df_info(df.fillna("missing"))<jupyter_output><empty_output><jupyter_text>Interpolate<jupyter_code>num_samples = 10

index = pd.date_range(
    "21/4/2021",
    periods=num_samples
)

values = 2 * np.sort(np.random.randn(num_samples)) + 1
df = pd.DataFrame(values, index)
df.iloc[2] = None
df.iloc[6] = None

df_info(df)
df.plot();
df_info(df.interpolate())
df.interpolate().plot();<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15038198632891034, 'fraction_numerical': 0.021310816244471252, 'mean_word_length': 4.923809523809524, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 26, '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': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '573223', 'license': 'MIT', 'n_tokens_mistral': 970, 'n_tokens_neox': 938, 'n_words': 209, 'path': '3_Pandas/03_MissingValues.ipynb', 'repo_name': 'tkaya94/UdemyDataScience', 'size': '2487'}