I have a Pandas dataframe containing np.nan (Numpy not-a-number) values:
field1
2020-12-24 NaN
2020-12-25 NaN
2020-12-26 1.0
2020-12-27 2.0
2020-12-28 NaN
2020-12-29 1.0
2020-12-30 2.0
(The index is a datetime.) I would like to obtain a new dataframe with the start date and the number of times np.nan occurs, i.e.
field1
2020-12-24 2
2020-12-28 1
I have tried this code:
prev = 1
for col_name, el in df.iterrows():
print(el)
if prev != np.nan and el[0] == np.nan:
cnt = 1
if prev == np.nan and el[0] == np.nan:
cnt = cnt + 1
if prev == np.nan and el[0] != np.nan:
print(cnt)
prev = el[0]
But it doesn't work as expected, and moreover I want to avoid "for" loops because I expect them to be very slow on bigger dataframes. Any help would be appreciated!