其他分享
首页 > 其他分享> > 删除带有任何/所有NaN值的行/列

删除带有任何/所有NaN值的行/列

作者:互联网

在带有熊猫的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

标签:pandas,data-table,filter,python,r
来源: https://codeday.me/bug/20191111/2023192.html