I am trying to fill some of the NaN values with values from another column and the row above.
For example, I get a dataframe that looks like this:
Distance Down firstDownYards secondDownYards
1 10.0 1.0 NaN NaN
2 8.0 2.0 2.0 NaN
3 8.0 3.0 2.0 0.0
4 19.0 3.0 -9.0 -11.0
5 19.0 4.0 -9.0 -11.0
6 10.0 1.0 NaN NaN
7 5.0 2.0 5.0 NaN
8 5.0 3.0 5.0 0.0
9 10.0 1.0 NaN NaN
10 9.0 2.0 1.0 NaN
11 11.0 3.0 -1.0 -2.0
12 12.0 4.0 -2.0 -3.0
13 10.0 1.0 NaN NaN
14 5.0 2.0 5.0 NaN
15 10.0 1.0 NaN NaN
16 8.0 2.0 2.0 NaN
17 8.0 3.0 2.0 0.0
18 10.0 1.0 NaN NaN
19 10.0 2.0 0.0 NaN
20 6.0 3.0 4.0 4.0
In the secondDownYards, I would like to fill the NaN where down is lower than 2 with the opposite of the next row of column firstDownYards. Here is an example of what the column would look like:
Distance Down firstDownYards secondDownYards
1 10.0 1.0 NaN -2 # Change here
2 8.0 2.0 2.0 NaN
3 8.0 3.0 2.0 0.0
4 19.0 3.0 -9.0 -11.0
5 19.0 4.0 -9.0 -11.0
6 10.0 1.0 NaN -5 # Change here
7 5.0 2.0 5.0 NaN
8 5.0 3.0 5.0 0.0
9 10.0 1.0 NaN -1 # Change here
10 9.0 2.0 1.0 NaN
11 11.0 3.0 -1.0 -2.0
12 12.0 4.0 -2.0 -3.0
13 10.0 1.0 NaN -5 # Change here
14 5.0 2.0 5.0 NaN
15 10.0 1.0 NaN -2 # Change here
16 8.0 2.0 2.0 NaN
17 8.0 3.0 2.0 0.0
18 10.0 1.0 NaN 0 # Change here
19 10.0 2.0 0.0 NaN
20 6.0 3.0 4.0 4.0
I have tried creating a function that looks like this, but when I try and print x.shift(), it simply prints the same thing as x. I would then use df.apply(getLastCol,args=(....),axis=1).
downNb is the condition, in this example being 2.
currentCol and lastCol are the names of the current column and the previous column.
def getLastCol(x,downNb,currentCol,lastCol):
if x['Down'] < downNb:
print(x.shift())
value = x.shift(-1)[lastCol]
else:
value = x[currentCol]
return value