Reputation: 126
I have a dataframe like this
>>df1 = pd.DataFrame({
'A': ['1', '2', '3', '4', '5'],
'B': ['1', '1', '1', '1', '1'],
'C': ['c', 'A1', NaN, 'c3', Nan],
'D': ['d0', 'B1', 'B2', Nan, 'B4'],
'E': ['A', Nan, 'S', Nan, 'S'],
'F': ['3', '4', '5', '6', '7'],
'G': ['2', '2', NaN, '2', '2']
})
>>df1
A B C D E F G
0 1 1 c d0 A 3 2
1 2 1 A1 B1 NaN 4 2
2 3 1 NaN B2 S 5 NaN
3 4 1 c3 NaN NaN 6 2
4 5 1 NaN B4 S 7 2
and I would like to get the coordinates of all nans. that is the output should be:
[[1,"E"], [2,"C"] , [2,"G"] , [3,"D"] ,[3,"E"] , [4,"C"] ]
All other questions i looked at just want the column name and not the pairs.
Is there any efficient way to solve this problem? Thank you
Upvotes: 3
Views: 807
Reputation: 61920
You could use np.argwhere with pd.isna, like this:
result = [[r, df1.columns[c]] for r, c in np.argwhere(pd.isna(df1).values)]
print(result)
Output
[[1, 'E'], [2, 'C'], [2, 'G'], [3, 'D'], [3, 'E'], [4, 'C']]
Upvotes: 1
Reputation: 4792
Try using np.where:
df = pd.DataFrame({'A': ['1', '2', '3', '4','5'],
'B': ['1', '1', '1', '1','1'],
'C': ['c', 'A1', np.nan, 'c3',np.nan],
'D': ['d0', 'B1', 'B2', np.nan,'B4'],
'E': ['A', np.nan, 'S', np.nan,'S'],
'F': ['3', '4', '5', '6','7'],
'G': ['2', '2', np.nan, '2','2']})
arr = np.where(df.isna())
arr
(array([1, 2, 2, 3, 3, 4], dtype=int64),
array([4, 2, 6, 3, 4, 2], dtype=int64))
np.where returns the indices where the given condition is True, here where df is null.
[(x, df.columns[y]) for x, y in zip(arr[0], arr[1])]
[(1, 'E'), (2, 'C'), (2, 'G'), (3, 'D'), (3, 'E'), (4, 'C')]
Upvotes: 1