I have a dataframe df
like this
| Time | variable one |
| -----------| -------------|
| 2022-11-09 | 0 |
| 2022-11-10 | 0 |
| 2022-11-11 | 2 |
| 2022-11-12 | 7 |
| 2022-11-13 | 0 |
| 2022-11-14 | 5 |
| 2022-11-15 | 3 |
| 2022-11-16 | 0 |
| 2022-11-17 | 0 |
I need to remove all the zeros before the first non zero element and to remove all the zeros after the last non zero element. Zeros in between non zero elements should remain zero.
I solved with two while loops:
i=0
while df.loc[i,'variable one']==0:
df.loc[i,'variable one'] = np.nan
i=i+1
i=len(df['variable one'])-1
while df.loc[i,'variable one']==0:
df.loc[i,'variable one'] = np.nan
i=i-1
This code works, but when dealing with hundreds of columns and many thousands rows it becomes very slow. I am looking for an optimization, even removing while loops.