better way to drop nan rows in pandas

0 votes

 I figured out a way to drop nan rows from a pandas dataframe. Given a dataframe dat with column x which contains nan values,is there a more elegant way to do drop each row of data which has a nan value in the x column?

dat = dat[np.logical_not(np.isnan(dat.x))]
dat = dat.reset_index(drop=True)

Jul 9 in Python by ana1504.k
• 7,890 points

1 answer to this question.

0 votes

Use dropna:


You can pass param how to drop if all labels are nan or any of the labels are nan

dat.dropna(how='any')    #to drop if any value in the row has a nan
dat.dropna(how='all')    #to drop if all values in the row are nan
answered Jul 9 by SDeb
• 13,210 points

Related Questions In Python

0 votes
1 answer

How to find the sum of rows in Pandas dataframe?

You can use a combination groupby function with the sum() method. ...READ MORE

answered May 9 in Python by Jisha
0 votes
1 answer

How to fill Null/NaN values in Pandas Dataframe?

Pandas allows you to change all the ...READ MORE

answered May 13 in Python by Rajat
0 votes
1 answer

What is the easiest way to implement 'IN' and 'NOT IN' in Pandas dataframe?

You can use pd.Series.isin. For "IN" use: something.isin(somewhere) Or for "NOT ...READ MORE

answered Aug 22 in Python by Arvind
• 2,960 points
0 votes
1 answer

What's the canonical way to check for type in Python?

To check if o is an instance ...READ MORE

answered Aug 24, 2018 in Python by Priyaj
• 56,960 points
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 394 views
0 votes
1 answer

Is arr.__len__() the preferred way to get the length of an array in Python?

my_list = [1,2,3,4,5,6,7] len(my_list) # 7 The same works for ...READ MORE

answered Oct 8, 2018 in Python by SDeb
• 13,210 points
0 votes
1 answer

Best way to open a socket in Python

Opening sockets in python is pretty simple. ...READ MORE

answered Feb 21 in Python by SDeb
• 13,210 points