I have a dataframe that looks like this :
Contract N° | 1-14 | 15-30 | 31-60 |
---|---|---|---|
10001 | NaN | NaN | 2500 |
10002 | NaN | 3500 | NaN |
10003 | NaN | NaN | NaN |
10004 | 1500 | NaN | NaN |
I would like to loop through all rows of this dataframe and on each row, loop from right to left while the value of the cell is NaN and counting the number of NaN cells thus found, then add a column to write down the number of continuous NaN cells counted.
Contract N° | 1-14 | 15-30 | 31-60 | NaN Count |
---|---|---|---|---|
10001 | NaN | NaN | 2500 | 0 |
10002 | NaN | 3500 | NaN | 1 |
10003 | NaN | NaN | NaN | 3 |
10004 | 1500 | NaN | NaN | 2 |
I feel like I'm mindlessly trying to translate VBA array logic into this:
for i in df:
While df[i,4-j] == 'NaN':
df['NaN Count'] = df['NaN Count']+1
j=j+1
I could not find a way to write a dataframe's cell coordinates as one would with a two-dimensional VBA array, kinda like df(i,j) so maybe this shouldn't be the way to go.
Any help would be greatly appreciated.