How to keep only rows containing a certain number of observation of a Data frame in python?

ipython

#1

I have created a Data frame in Ipython notebook .I want to know how to get the rows in which only fixed number of column has data and other has missing value.

df = DataFrame(np.random.randn(7, 3))
df.ix[:4, 1] = NA; df.ix[:2, 2] = NA

df 

0 1 2 0 1 2
0 -0.577087 NaN NaN 
1 0.523772 NaN NaN 
2 -0.713544 NaN NaN
3 -1.860761 NaN 0.560145
4 -1.265934 NaN -1.063512
5 0.332883 -2.359419 -0.199543
6 -1.541996 -0.970736 -1.307030

I want get only those rows in which only one missing data is present.


#2

@sid100158,

I would suggest you to use isnull() with each column and convert it to 0 or 1 by multiplying with 1. Next, add all values and store it to additional columns, you can also use loop for same. Below, I have done this simply as here we have only three columns. Additional column have values for number of missing values in respective rows, now you can play with it to slice data.

df[3]=df[0].isnull()*1+df[1].isnull()*1+df[2].isnull()*1
df[df[3]==1]

Regards,
Mark