zsljulius
zsljulius

Reputation: 4113

Retrive the (index,column) tuple of non nan values

Suppose I have this dataframe

pd.DataFrame([[1,np.nan,np.nan],[np.nan,np.nan,np.nan],[np.nan,6,np.nan]])

The function should return me the array of tuples:

 [(0,0), (2,1)]

Upvotes: 1

Views: 1204

Answers (1)

Daniel
Daniel

Reputation: 19547

You can use the numpy functions isnan and where:

>>> df = pd.DataFrame([[1,np.nan,np.nan],[np.nan,np.nan,np.nan],[np.nan,6,np.nan]])
>>> np.where(~np.isnan(df))
(array([0, 2]), array([0, 1]))

To obtain the data in the exact way as shown:

>>> inds = np.where(~np.isnan(df))
>>> zip(*inds)
[(0, 0), (2, 1)]

Using pandas built in functions you would have to apply notnull() over all series and then call a numpy function to the DataFrame anyway.

Edit: Apparently pandas has a notnull function for DataFrames in 0.13, you can replace all ~np.isnan(df) with df.notnull() if you wish.

Upvotes: 5

Related Questions