![](/img/trans.png)
[英]How to remove rows from dataframe if column values don't match with all the values on the list
[英]How to remove rows from a dataframe if 75 % of its column values is equal to 0
我有一個44列和60,000行的數據框。 我想刪除那些行,如果它有0到75%的列。 這75%:例如,在我的案例中,有44列是其33列。 因此,我在R中嘗試了以下功能,
filter <- apply(df, 1,function(x) any(x[1:33]!=0) && any(x[34:44]!=0) )
df = df[filter,]
正是我想要的那些列。 但是問題是我的數據框有很多這樣的行,對於某些行,替代模型中有零,即一列是數字,然后是零,依此類推。 有時多於33列,並且上面的函數避免了這些行。
到目前為止,我在R中嘗試過,我可以在熊貓中嘗試的任何解決方案也都很棒。.我知道當熊貓中的所有值都不等於零時
df[(df != 0).all(1)]
這是我的數據框的樣子,
dim(df)
[1] 57905 44
head(df)
ID Pe_1 Pe_2 Pe_3 Pe_4 Pe_5 Pe_6 Pe_7 Pe_8 Pe_9 Pe_10 Pe_11 Pe_12 Pe_13 Pe_14 Pe_15 Pe_16 Pe_17 Pe_18 Pe_19 Pe_20 Pe_21 Pe_22 Pe_23 Pe_24 Pe_25 Pe_26 Pe_27 Pe_28 Pe_29 Pe_30 Pe_31 Pe_32 Pe_33 Pe_34 Pe_35 Pe_36 Pe_37 Pe_38 Pe_39 Pe_40 Pe_41 Pe_42 Pe_43 Pe_44
ENSG1 0 0 1 0 0 2 2 1 0 0 0 1 0 3 3 0 1 0 2 0 2 3 1 2 0 2 0 0 0 0 0 2 0 0 0 0 2 0 0 2 0 3 1 3
ENSG2 274 293 300 273 229 124 427 291 274 561 128 506 342 540 376 422 411 190 723 224 303 316 766 697 251 167 271 361 325 133 215 274 217 366 227 579 337 254 570 188 143 363 250 359
ENSG3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
ENSG4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
ENSG5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
ENSG6 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
ENSG7 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
ENSG8 0 1 0 1 1 1 0 2 0 0 0 1 1 1 0 1 0 0 0 0 0 1 1 1 2 1 0 3 0 1 1 2 0 0 0 0 0 0 1 1 0 0 1 1
ENSG9 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
ENSG10 3 2 4 6 21 6 6 13 3 1 1 6 10 4 2 0 1 0 0 0 4 2 5 3 25 9 7 10 7 5 3 0 0 5 1 8 4 5 0 4 1 3 2 4
ENSG11 277 43 79 216 1170 174 213 1303 564 14 53 76 170 1016 32 19 69 69 50 21 75 31 560 86 2668 604 513 303 1378 109 219 172 10 1031 276 242 1587 217 76 43 450 81 502 99
任何建議/幫助都很好
似乎您要刪除大於0
75%的行。 例如,保留至少具有25%非零值的行。
在R
:
df = data.frame(a=c(1,8,0), b=c(0,2,0), c=c(0,0,1), d=c(4,4,0))
df[rowMeans(df!=0)>0.25, ] # or df[rowMeans(df==0)<0.75, ]
# a b c d
#1 1 0 0 4
#2 8 2 0 4
而在Pandas
:
df = pd.DataFrame({'a':[1,8,0],'b':[0,2,0],'c':[0,0,1], 'd':[4,4,0]})
# In [198]: df
# Out[198]:
# a b c d
#0 1 0 0 4
#1 8 2 0 4
#2 0 0 1 0
df[df.astype('bool').mean(axis=1)>=0.25] # or df[(~df.astype('bool')).mean(axis=1)<0.75]
#Out[199]:
# a b c d
#0 1 0 0 4
#1 8 2 0 4
試試這個(熊貓):
df[(df==0).sum(axis=1)/len(df.columns) <= 0.75]
Pandas方法,這里我們將df與0
進行比較,並使用axis=1
逐行sum
,這將產生一個具有0值計數的Series
,然后將其與75%的行長進行比較並過濾df:
In [14]:
df[(df == 0).sum(axis=1) < df.shape[1] * 0.75]
Out[14]:
ID Pe_1 Pe_2 Pe_3 Pe_4 Pe_5 Pe_6 Pe_7 Pe_8 Pe_9 ... \
0 ENSG1 0 0 1 0 0 2 2 1 0 ...
1 ENSG2 274 293 300 273 229 124 427 291 274 ...
7 ENSG8 0 1 0 1 1 1 0 2 0 ...
9 ENSG10 3 2 4 6 21 6 6 13 3 ...
10 ENSG11 277 43 79 216 1170 174 213 1303 564 ...
Pe_35 Pe_36 Pe_37 Pe_38 Pe_39 Pe_40 Pe_41 Pe_42 Pe_43 Pe_44
0 0 0 2 0 0 2 0 3 1 3
1 227 579 337 254 570 188 143 363 250 359
7 0 0 0 0 1 1 0 0 1 1
9 1 8 4 5 0 4 1 3 2 4
10 276 242 1587 217 76 43 450 81 502 99
[5 rows x 45 columns]
或analgously到@上校beauvel的使用rowMeans
可以使用rowSums
。
df[rowSums(df[, -1] > 0) / (ncol(df)-1) >= 0.75,]
rowSums
將所有行加在一起(it和rowMeans
超級快)。 ncol
返回df的列數(我從中減去1)。 rowSums
和ncol
進行比較,0.75返回邏輯。 此邏輯用於子集行。
簡單的代碼,它應該可以工作:
for i in df:
iLength = 0
countZeros = 0
for j in df:
iLength += 1
if i[j] == 0:
countZeros += 1
zeroRate = countZeros / iLength
if zeroRate >= 0.75:
#DeleteRow
R解決方案(希望如此)
我想我明白了,略過您想要的作品。 在這里和那里之間的代碼都是上下文,可以在R中看到。
MakeDF.R <- function(CustomVector,n){
#just a function to make a sample df
NewDF <- data.frame(matrix(nrow=n,ncol=length(CustomVector)))
colnames(NewDF) <- CustomVector
return(NewDF)
}
制作數據框
DF<-MakeDF.R(c(1:44),10)
#sample df with 44 rows
用零添加一些行
DF[c(5,6,7),] <- c(1:44)*0
#add sample zero rows
現在就您想要的。
RemoveRows <- lapply(1:nrow(DF),function(i){
RemoveRow_i = 0
if(
length(which(DF[i,] == 0)) > (ncol(DF) * 0.75) ){
#above is the "more than 75% zero columns step
RemoveRow_i = i #select this row as one to be removed
}
RemoveRow_i
#return either the row number or zero
})
這給了我們要刪除的行列表。 現在我們需要清理該列表(刪除零)
RemoveRows = RemoveRows[RemoveRows > 0]
#Leaves no zeroes in the list
清理列表后,我們可以刪除數據
CleanedDF <- DF[-do.call(rbind,RemoveRows)[,1],]
#the do.call(rbind....) is returning a 1 column dataframe.
#So the first column is the vector of rows we want to remove.
#the -c(row numbers) removes rows. df[-1,] would delete the first row
#df[-c(1,2),] would delete the first two rows
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.