Reputation: 823
Hi folks I have this simple problem, where I am trying to find the iloc row postion of all NaN field in a dataframe (column B). So far I am solving the problem the following way:
rng= ['AA', 'BB', 2, 3, 4, 5]
df1 = pd.DataFrame(np.random.randn(6, 3), index=rng, columns=['A', 'B', 'C'])
df1.iloc[1][1]= np.nan
+----------------------------------+
| A B C |
+----------------------------------+
| AA 0.198267 -1.469309 -1.751756 |
| BB -1.376388 Nan 0.988391 |
| 2 -1.697636 -0.814975 0.614170 |
| 3 -1.187977 1.240791 -1.079049 |
| 4 -1.495139 0.215619 -1.572205 |
| 5 1.157736 -0.656647 -0.307207 |
+----------------------------------+
ind_com=df1.loc[df1.B.isnull()].index.values.tolist()
ind_list=[]
for ii in ind_com:
ind_list.append(df_temp.index.get_loc(ii))
ind_list = 1 Surely there must be a better way. Thank you
Upvotes: 1
Views: 65
Reputation: 2394
Since the iloc really just a 0-indexed row number, it should be identical to enumerating over the nulls in the column:
[iloc for iloc, null in enumerate(df['B'].isnull()) if null]
Upvotes: 1
Reputation: 863236
I think you need:
pos = [df1.index.get_loc(x) for x in df1.index[df1.B.isnull()]]
Another solution with numpy.where
:
pos = np.where(df1.B.isnull().values)[0].tolist()
Or numpy.nonzero
:
pos = np.nonzero(df1.B.isnull().values)[0].tolist()
Upvotes: 2
Reputation: 153500
Let's a different approach (no looping):
ind_list = np.where(df1.B.isnull().as_matrix())[0].tolist()
print(ind_list)
Upvotes: 2