<jupyter_start><jupyter_text>Dealing with Missing Values (NAs)<jupyter_code>#create sample data
df = pd.DataFrame([[1,np.nan,2],[2,3,5],[np.nan,4,6]]) #data has no values (NAs) in some cells
#we need to remove NA or NaN from the data frame
print(df)
## check if the data frames contains NA
print(df.isnull())
## Drop NA values
## all rows with cells containing NA will be dropped
print(df.dropna())
## Drop columns where cells have NAs
print(df.dropna(axis=1))
print(df.dropna(axis=1,how="all"))
## thresh parameter lets you specify a minimum number of non-null values for the row/column to be kept
print(df.dropna(thresh=2))<jupyter_output>0    1  2
0  1.0  NaN  2
1  2.0  3.0  5
2  NaN  4.0  6<jupyter_text>Filling no values (NA) cells<jupyter_code>#fill NA entries with zero
print(df.fillna(0))
#specify a forward-fill to propagate the previous value forward
print(df.fillna(method="ffill"))
#fill forward column wise
print(df.fillna(method="ffill",axis=1))
#back-fill to propagate the next values backward
print(df.fillna(method="bfill"))<jupyter_output>0    1  2
0  1.0  3.0  2
1  2.0  3.0  5
2  NaN  4.0  6
{'fraction_non_alphanumeric': 0.12320143884892086, 'fraction_numerical': 0.04316546762589928, 'mean_word_length': 4.766839378238342, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '633978', 'license': 'MIT', 'n_tokens_mistral': 429, 'n_tokens_neox': 418, 'n_words': 136, 'path': 'section3/removena.ipynb', 'repo_name': 'sanjanapackt/PacktPublishing-Clustering-and-Classification-with-Machine-Learning-in-Python', 'size': '1112'}