在 pandas 数据框中使用NaN查找行的整数索引 [英] Find integer index of rows with NaN in pandas dataframe
本文介绍了在 pandas 数据框中使用NaN查找行的整数索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个这样的熊猫DataFrame:
I have a pandas DataFrame like this:
a b
2011-01-01 00:00:00 1.883381 -0.416629
2011-01-01 01:00:00 0.149948 -1.782170
2011-01-01 02:00:00 -0.407604 0.314168
2011-01-01 03:00:00 1.452354 NaN
2011-01-01 04:00:00 -1.224869 -0.947457
2011-01-01 05:00:00 0.498326 0.070416
2011-01-01 06:00:00 0.401665 NaN
2011-01-01 07:00:00 -0.019766 0.533641
2011-01-01 08:00:00 -1.101303 -1.408561
2011-01-01 09:00:00 1.671795 -0.764629
是否存在一种有效的方法来查找具有NaN的行的整数"索引?在这种情况下,所需的输出应为[3, 6]
.
Is there an efficient way to find the "integer" index of rows with NaNs? In this case the desired output should be [3, 6]
.
推荐答案
对于DataFrame df
:
For DataFrame df
:
import numpy as np
index = df['b'].index[df['b'].apply(np.isnan)]
将带回您可以用来索引回df
的MultiIndex
,例如:
will give you back the MultiIndex
that you can use to index back into df
, e.g.:
df['a'].ix[index[0]]
>>> 1.452354
对于整数索引:
df_index = df.index.values.tolist()
[df_index.index(i) for i in index]
>>> [3, 6]
这篇关于在 pandas 数据框中使用NaN查找行的整数索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文