Back
Close

Computing with Data

elgeish
33K views
Previous: Missing Data in R - Part II Next: Outliers

Missing Data in Python

The code below defines a small dataframe with missing values, and demonstrates the isnull, dropna, and fillna methods:

import numpy as np
import pandas as pd
data = pd.DataFrame([[1, 2, np.nan],
[3, np.nan, 4],
[1, 2, 3]])
# Example 1 - the isnull method
print(f"data:\n{data}")
print("pd.isnull(data):")
print(pd.isnull(data))
# Example 2 - the dropna method
# Drop rows
print("data.dropna():")
print(data.dropna())
# Drop columns
print("data.dropna(axis = 1):")
print(data.dropna(axis = 1))
# Example 3 - the fillna method
# Fill in missing entries with column means
print("data.fillna(data.mean()):")
print(data.fillna(data.mean()))
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Go to tech.io
codingame x discord
Join the CodinGame community on Discord to chat about puzzle contributions, challenges, streams, blog articles - all that good stuff!
JOIN US ON DISCORD
Online Participants