我想使用滚动计数,最大值为36,需要包含NaN值,例如,如果是NaN,则从0开始。我的数据框看起来像这样:
输入:
val
NaN
1
1
NaN
2
1
3
NaN
5
代码:
b = a.rolling(36,min_periods=1).apply(lambda x: len(np.unique(x))).astype(int)
它给了我:
Val count
NaN 1
1 2
1 2
NaN 3
2 4
1 4
3 5
NaN 6
5 7
预期产出:
Val count
NaN 0
1 1
1 1
NaN 1
2 2
1 2
3 3
NaN 3
5 4
答案 0 :(得分:4)
您只需过滤掉var numarray = new Array(76); // Create new array with 76 indexed positions (or, a .length of 76)
// Start a loop
do {
// Math.random() - Get a random number between 0 (inclusive) and 1 (exclusive)
// * 76 - Take the random and multiply by 76 to get a random between 0 (inclusive)
// and 76 (exclusive)
// Math.floor() - Round the number down to the next whole number
// + 1 - Instead of the range being 0 (inclusive) and 76 (exclusive) add an offset
// so that the final number will be between 1 (inclusive) and 77 (exclusive)
var rannum = Math.floor(Math.random() * 76) + 1;
} while (numarray[rannum]); // Keep the loop going as long as the array item matching the random
// isn't undefined, false, 0, NaN, null or "" (i.e. "truthy").
// In this case, the array is empty so the loop will only
// iterate one time.
// Set the array item that matches the gNumber value to true.
// You haven't provided any code that declares or initializes gNumber,
// so this line of code really is meaningless in this context.
numarray[gNumber] = true;
// Change the inner content (HTML and text) of the element with an id that matches
// the value in cellID to the random number
document.getElementById(cellID).innerHTML = rannum;
nan
原因
df.val.rolling(36,min_periods=1).apply(lambda x: len(np.unique(x[~np.isnan(x)]))).fillna(0)
Out[35]:
0 0.0
1 1.0
2 1.0
3 1.0
4 2.0
5 2.0
6 3.0
7 3.0
8 4.0
Name: val, dtype: float64