2017-05-31 57 views
2

例如, 我有这样一些数据:如何找到楠之前的第一个非NAN数据在一列中熊猫

column = pd.Series([1,2,3,np.nan,4,np.nan,7]) 
print column 

执行的命令,结果是这样的:

0 1.0 
1 2.0 
2 3.0 
3 NaN 
4 4.0 
5 NaN 
6 7.0 

现在我想知道每个NaN值之前的第一个值是什么,例如在第一个NaN之前的第三个值。而4.0是第二个NaN值之前的结果。熊猫中是否有内置函数可以完成此操作,或者我应该写一个for循环来执行此操作?

回答

1

同样的想法作为@jezrael ... numpy fied。

column[np.append(np.isnan(column.values)[1:], False)] 

2 3.0 
4 4.0 
dtype: float64 

完成与pd.Series重建

m = np.append(np.isnan(column.values)[1:], False) 
pd.Series(column.values[m], column.index[m]) 

2 3.0 
4 4.0 
dtype: float64 

几乎没有那么快,但直观。由isnullcumsum分组并取最后一个值。这个结果,摆脱最后一行。

column.groupby(column.isnull().cumsum()).last().iloc[:-1] 

0 3.0 
1 4.0 
dtype: float64 
+0

非常感谢。 – Dogod

2

与非连续工作的解决方案NaN s。通过mul

print (column[column.isnull().shift(-1).fillna(False)]) 
2 3.0 
4 4.0 
dtype: float64 

print (column.isnull()) 
0 False 
1 False 
2 False 
3  True 
4 False 
5  True 
6 False 
dtype: bool 

print (column.isnull().shift(-1)) 
0 False 
1 False 
2  True 
3 False 
4  True 
5 False 
6  NaN 
dtype: object 

print (column.isnull().shift(-1).fillna(False)) 
0 False 
1 False 
2  True 
3 False 
4  True 
5 False 
6 False 
dtype: bool 

随着连续NaN的需要倒置c多个:
可以使用boolean indexingisnullshiftfillna形成的遮掩

column = pd.Series([np.nan,2,3,np.nan,np.nan,np.nan,7,np.nan, np.nan, 5,np.nan]) 

c = column.isnull() 
mask = c.shift(-1).fillna(False).mul(~c) 
print (mask) 
0  False 
1  False 
2  True 
3  False 
4  False 
5  False 
6  True 
7  False 
8  False 
9  True 
10 False 
dtype: bool 

print (column[mask]) 
2 3.0 
6 7.0 
9 5.0 
dtype: float64 
+0

非常感谢。 – Dogod

相关问题