1

How can I get all the Positions in Pandas where there are NAN values. For Example,

sample = pd.DataFrame(np.zeros(shape=[5,5]))
sample.iloc[0,0] = np.nan
sample.iloc[2,3] = np.nan
sample.iloc[4,3] = np.nan

is a Pandas Data Frame where the Positions such as (0,0), (2,3) and(4,3) have nan Values. Consequently, I want a list of Tuples like this

[(0,0),(2,3),(4,3)]

Ho can I get this ?

Regards

smci
  • 32,567
  • 20
  • 113
  • 146
Sibghat Khan
  • 147
  • 1
  • 9
  • There are many existing duplicates, please use the search function. – smci Feb 10 '17 at 00:53
  • or invert [How to get row, column indices of all non-NaN items in Pandas dataframe](http://stackoverflow.com/questions/36375939/how-to-get-row-column-indices-of-all-non-nan-items-in-pandas-dataframe) – smci Feb 10 '17 at 00:57

1 Answers1

3

In this case since your column and index values are the row and column numbers you can first turn each entry to boolean using isnull and then filter for just the true values in the brackets with a lambda function and then turn the selected indices into a list.

sample.isnull().stack()[lambda x: x].index.tolist()
[(0, 0), (2, 3), (4, 3)]
Ted Petrou
  • 59,042
  • 19
  • 131
  • 136