简体   繁体   中英

What is the best way to get Index of NANs in a pandas data Series

Suppose we've got some data series:

0  'one'
1  'two'
2   NAN
3   'three'
4   NAN
5   NAN

Now I would like to get the indecies of all the NAN elements. So using python's pandas lib I would do something like that:

import pandas as pd
import numpy as np


data = pd.Series(['one', 'two', np.nan, 'three', np.nan, np.nan])
nan_index = data.index.difference(data.dropna().index)

However, I'm getting the feeling that it's not a pandonic way of doing it.

By using isnull

data[data.isnull()].index
Out[739]: Int64Index([2, 4, 5], dtype='int64')

Or

data.isnull().nonzero()
In [11]: data.index[data.isnull()]
Out[11]: Int64Index([2, 4, 5], dtype='int64')

or

In [12]: np.where(data.isnull())[0]
Out[12]: array([2, 4, 5], dtype=int64)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM