我有一个包含几列的数据框。
我想获取两列值都不是 NaN 的第一行索引。
我知道我需要使用df.first_valid_index()
示例: 如果我的数据框是:
Date A B
0 2015-01-02 NaN 1
1 2015-01-02 NaN 2
2 2015-01-02 NaN 3
3 2015-01-02 1 NaN
5 2015-01-02 NaN 2
7 2015-01-03 1 1
我会得到7
最佳答案
一种方法 -
(~(pd.isnull(df.A) | pd.isnull(df.B))).idxmax()
类似的 -
(~pd.isnull(df[['A','B']]).any(1)).idxmax()
为了性能,我们可能需要使用argmax
-
(~pd.isnull(df[['A','B']]).any(1)).argmax()
纯粹考虑性能,我们可以引入更多 NumPy -
df.index[(~(np.isnan(df.A.values) | np.isnan(df.B.values))).argmax()]
示例运行 -
In [172]: df
Out[172]:
Date A B
0 2015-01-02 NaN 1.0
1 2015-01-02 NaN 2.0
2 2015-01-02 NaN 3.0
3 2015-01-02 1.0 NaN
5 2015-01-02 NaN 2.0
7 2015-01-03 1.0 1.0
In [173]: (~(pd.isnull(df.A) | pd.isnull(df.B))).idxmax()
Out[173]: 7
In [180]: (~pd.isnull(df[['A','B']]).any(1)).idxmax()
Out[180]: 7
In [182]: (~pd.isnull(df[['A','B']]).any(1)).argmax()
Out[182]: 7
In [258]: df.index[(~(np.isnan(df.A.values) | np.isnan(df.B.values))).argmax()]
Out[258]: 7
运行时测试 -
In [259]: a = np.random.rand(100000,2)
In [260]: a[np.random.rand(*a.shape)>0.2] = np.nan
In [261]: df = pd.DataFrame(a, columns=list(('A','B')))
# @jezrael's soln
In [262]: %timeit df[['A','B']].notnull().all(axis=1).idxmax()
100 loops, best of 3: 4.91 ms per loop
In [263]: %timeit (~(pd.isnull(df.A) | pd.isnull(df.B))).idxmax()
...: %timeit (~pd.isnull(df[['A','B']]).any(1)).idxmax()
...: %timeit (~pd.isnull(df[['A','B']]).any(1)).argmax()
...:
1000 loops, best of 3: 1.37 ms per loop
100 loops, best of 3: 4.73 ms per loop
100 loops, best of 3: 4.74 ms per loop
In [264]: %timeit df.index[(~(np.isnan(df.A.values) | np.isnan(df.B.values))).argmax()]
10000 loops, best of 3: 169 µs per loop
关于python - 如何获取两列都不为空的 pandas 数据帧的第一个索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43517993/