Last active
April 16, 2020 05:32
-
-
Save romainx/34a6ce0caeb6fd6e8e88fb192e9e0355 to your computer and use it in GitHub Desktop.
[Drop NA] Drop rows containing missing values NA in a data frame #python #R
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Refs: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.dropna.html | |
import pandas as pd | |
import numpy as np | |
df = pd.DataFrame({"x": [1, 2, np.nan], | |
"y": ["a", np.nan, "b"] | |
}) | |
# Drop each row containing at least a NA | |
df.dropna() | |
# x y | |
# 0 1.0 a | |
# Drop each row where the x column contains a NA | |
df.dropna(subset=["x"]) | |
# x y | |
# 0 1.0 a | |
# 1 2.0 NaN |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Refs: https://tidyr.tidyverse.org/reference/drop_na.html | |
library(dplyr) | |
library(tidyr) | |
df <- tibble(x = c(1, 2, NA), y = c("a", NA, "b")) | |
# Drop each row containing at least a NA | |
df %>% drop_na() | |
# # A tibble: 1 x 2 | |
# x y | |
# <dbl> <chr> | |
# 1 1 a | |
# Drop each row where the x column contains a NA | |
df %>% drop_na(x) | |
# # A tibble: 2 x 2 | |
# x y | |
# <dbl> <chr> | |
# 1 1 a | |
# 2 2 NA |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment