在带有 pandas 的 python 中,我可以执行以下操作:
# Drop columns with ANY missing values
df2 = df.dropna(axis=1, how="any")
# Drop columns with ALL missing values
df2 = df.dropna(axis=1, how="all")
# Drop rows with ANY missing values
df2 = df.dropna(axis=0, how="any")
# Drop rows with ALL missing values
df2 = df.dropna(axis=0, how="all")
我如何以类似的方式过滤 R data.table 中的行/列?
最佳答案
我们可以将 Reduce
与 |
或 &
一起使用
library(data.table)
#Drop rows with any missing values
setDT(df1)[df1[, !Reduce(`|`, lapply(.SD, is.na))]]
#Drop rows with all missing values
setDT(df1)[df1[, !Reduce(`&`, lapply(.SD, is.na))]]
#Drop columns with any and all missing values
Filter(function(x) !any(is.na(x)), df1)
Filter(function(x) !all(is.na(x)), df1)
#or use
setDT(df1)[, unlist(df1[, lapply(.SD, function(x) any(!is.na(x)))]), with = FALSE]
setDT(df1)[, unlist(df1[, lapply(.SD, function(x) all(!is.na(x)))]), with = FALSE]
数据
set.seed(24)
df1 <- as.data.table(matrix(sample(c(NA, 0:5), 4*5, replace=TRUE), ncol=4))
df1[3] <- NA
关于python - 删除具有任何/所有 NaN 值的行/列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41343900/