Reputation: 1253
I have a data frame like this,
ID Cus_ID cl_id
0 5.0 200 0
1 NaN 200 0
2 NaN 200 1
3 14.0 200 2
4 15.0 200 2
5 16.0 200 2
6 NaN 200 3
From dataframe above, I am want to extract rows 0 through 4 and check if there is any values in 'ID' column has a NaN
values. I tried this,
rows_needed = [0,1,2,3,4]
df.iloc[rows_needed,0].isnull().index.tolist()
But I get the following,
[0, 1, 2, 3, 4]
I am expecting to get the indexes of [1,2]
. How can I get my desired output?
When I do this,
df.iloc[rows_needed,0].isnull()
I get,
0 False
1 True
2 True
3 False
4 False
Name: ID, dtype: bool
Not sure where I am making the mistake not to get my output.
Upvotes: 3
Views: 596
Reputation: 25249
You may use index.symmetric_different
with dropna
to find those index is not NaN
as follows:
df.iloc[rows_needed,0].dropna().index.symmetric_difference(rows_needed).tolist()
or
(df.iloc[rows_needed,0].dropna().index ^ rows_needed).tolist()
Out[684]: [1, 2]
Upvotes: 1
Reputation: 51155
Two steps for clarity. Slice, then mask based on that slice.
u = df.iloc[rows_needed, 0]
u[u.isnull()].index.tolist()
[1, 2]
Upvotes: 4
Reputation: 23099
You're very very close, what you need to do is chain .iloc
and .loc
with a ==TRUE
to get your result
your_indices = (df.iloc[rows_needed]
.loc[df.ID.isnull()==True]
.index.tolist())
print(your_indices)
[1, 2]
Upvotes: 6
Reputation: 323276
Let us chain it loc
will only pick the result yield True out
rows_needed = [0,1,2,3,4]
df.iloc[rows_needed,0].isnull().loc[lambda x : x].index.tolist()
Out[240]: [1, 2]
Upvotes: 3