I am learning how to use for loops with if statements, can someone tell why python is only reading the if statement and not the elif statements.
mydf = [['house',7,5, np.nan],['block',30,25,19],['else',20, np.nan, np.nan]]
mydf= pd.DataFrame(mydf, columns=['Thing','height1','height2','height3'])
I am trying to create a loop that will go each line and first check: if index in height3 is NOT NaN then put that value in bottomHeight. Else if index in height2 is NOT NaN then put that value in bottomHeight. Or else put NaN in bottomHeight.
#Create a new column of NaN's
mydf["bottomHeight"]=(np.nan)*len(mydf)
for index in range(len(mydf)):
if mydf.loc[index,'height3'] != np.nan:
mydf.loc[index,'bottomHeight'] = mydf.loc[index, 'height3']
elif mydf.loc[index,'height2'] != np.nan:
mydf.loc[index,"bottomHeight"] = mydf.loc[index, 'height2']
else:
mydf.loc[np.nan,'bottomHeight'] = np.nan
The result should be bottomHeight = [5.0, 19.0, NaN], but it's not. The result is [NaN, 19.0, NaN]. Like it's only reading the first if statement.