Skip to content

Instantly share code, notes, and snippets.

@yohanesnuwara
Created March 24, 2020 05:54
Show Gist options
  • Save yohanesnuwara/07de55b8cc3939632107c54127b5d573 to your computer and use it in GitHub Desktop.
Save yohanesnuwara/07de55b8cc3939632107c54127b5d573 to your computer and use it in GitHub Desktop.
Check and remove NaN values in DataFrame
import pandas as pd
# create dataframe from data
data = pd.DataFrame({"Column_1": numpy_1, "Column_2": numpy_2, "Column_3": numpy_3})
# print original size of data before NaN values removal
print("Original size of data:", len(data), "rows")
# look up the number of rows with NaN data
print("How many data with NaN values?")
print(data.isnull().sum())
# drop rows with missing values
data.dropna(inplace=True)
# after removal, the index will be not set from 0
# re-index dataframe (in order, started from 0)
data = data.reset_index(drop=True)
print("NaN has successfully been deleted")
# see data after removal
data.head(10)
@yohanesnuwara
Copy link
Author

yohanesnuwara commented Mar 24, 2020

Sample result:

Original size of data: 46 rows
How many data with NaN values?
UTM_X       0
UTM_Y       0
Distance    0
CBA         4
dtype: int64
NaN has successfully been deleted

  | UTM_X | UTM_Y | Distance | CBA
-- | -- | -- | -- | --
290472.095982 | 4.233793e+06 | 3870.466226 | -205.706931
291979.551622 | 4.235007e+06 | 5805.699339 | -210.851841

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment