Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove row with nan value

let's say, for example, i have this data:

data <- c(1,2,3,4,5,6,NaN,5,9,NaN,23,9) attr(data,"dim") <- c(6,2) data       [,1] [,2] [1,]    1  NaN [2,]    2    5 [3,]    3    9 [4,]    4  NaN [5,]    5   23 [6,]    6    9 

Now i want to remove the rows with the NaN values in it: row 1 and 4. But i don't know where these rows are, if it's a dataset of 100.000+ rows, so i need to find them with a function and remove the complete row.

Can anybody point me in the right direction?

like image 770
Sir Ksilem Avatar asked May 11 '11 09:05

Sir Ksilem


People also ask

How do I remove a row from a NaN value?

To drop all the rows with the NaN values, you may use df. dropna().

How do you delete a row with no value in Python?

DataFrame. dropna() also gives you the option to remove the rows by searching for null or missing values on specified columns. To search for null values in specific columns, pass the column names to the subset parameter.

How do I drop a row if all NaN pandas?

Pandas Drop Rows Only With NaN Values for All Columns Using DataFrame. dropna() Method. It removes only the rows with NaN values for all fields in the DataFrame. We set how='all' in the dropna() method to let the method drop row only if all column values for the row is NaN .


1 Answers

The function complete.cases will tell you where the rows are that you need:

data <- matrix(c(1,2,3,4,5,6,NaN,5,9,NaN,23,9), ncol=2) data[complete.cases(data), ]       [,1] [,2] [1,]    2    5 [2,]    3    9 [3,]    5   23 [4,]    6    9 
like image 82
Andrie Avatar answered Sep 23 '22 05:09

Andrie