我有一个与前一个问题有关的问题:Identifying consecutive NaN's with pandas
我是stackoverflow的新手,因此无法添加注释,但我想知道,在计算连续NaN的数量时,如何部分保留数据帧的原始索引
因此,不是:
df = pd.DataFrame({'a':[1,2,np.NaN, np.NaN, np.NaN, 6,7,8,9,10,np.NaN,np.NaN,13,14]})
df
Out[38]:
a
0 1
1 2
2 NaN
3 NaN
4 NaN
5 6
6 7
7 8
8 9
9 10
10 NaN
11 NaN
12 13
13 14
我希望获得以下资料:
Out[41]:
a
0 0
1 0
2 3
5 0
6 0
7 0
8 0
9 0
10 2
12 0
13 0
我找到了一个解决办法。它很难看,但它确实起了作用。我希望您没有海量数据,因为它的性能可能不太好:
NaN group thingy受到以下答案的启发:这来自Thisanswer
相关问题 更多 >
编程相关推荐