python - 删除具有任何/所有 NaN 值的行/列

标签 python r pandas filter data.table

在带有 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/

相关文章:

python - SKlearn 导入 MLPClassifier 失败

r - 选择第二个空格后的字符串

r - 根据特定模式按行分割数据帧

r - 用于逻辑回归的 GLM 函数 : what is the default predicted outcome?

python - 如何提取特定类别之前的最后 3 个索引号

python - 如何在 Spyder 3.6 中安装 GraphViz

python - 重新加载模块后在 Python 中进行对象类型转换? [用于即时代码更改]

python - 使用python查找适合文件中像素的物理坐标

python - 如何仅保留 pandas 数据帧每组的前 n% 行?

python - Season_decompose 引发错误 : TypeError: PeriodIndex given. 检查 `freq` 属性而不是使用 infer_freq