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, 2019 in Python by ana1504.k
• 7,910 points
4,512 views

1 answer to this question.

0 votes

Use dropna:

dat.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, 2019 by SDeb
• 13,300 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, 2019 in Python by Jisha
4,608 views
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, 2019 in Python by Rajat
6,860 views
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 23, 2019 in Python by Arvind
• 3,050 points
12,774 views
0 votes
1 answer

How to check if any value is NaN in a Pandas DataFrame?

Hello @kartik, If you need to know how ...READ MORE

answered Jun 15, 2020 in Python by Niroj
• 82,840 points
4,285 views
0 votes
2 answers
+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 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,434 views
0 votes
1 answer
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,300 points
947 views
0 votes
1 answer

Best way to open a socket in Python

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

answered Feb 21, 2019 in Python by SDeb
• 13,300 points
788 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP