我想在列的连续数字之间重命名零。例如,我需要输出A看起来像输出Anew。
A = [1、0、0、0、2、0、3、0、0、0、0、0、4]
A =
1 0 0 0 2 0 3 0 0 0 0 0 4
重新= [1、1、1、1、2、2、3、3、3、3、3、3、4]
重新=
1 1 1 1 2 2 3 3 3 3 3 3 4
任何帮助将不胜感激,并在此先感谢:)
If they are consecutive positive numbers you can use
cummax
:Replace zeros with
NaN
s and then usefillmissing
to replaceNaN
s with the previous non-NaN
value.or as a one-liner using
standardizeMissing
withfillmissing
: