I am attempting to create a computed field in pandas, and I was given two formulas. Due to data issues, when one formula would result in a nan, I should be using the other. However, I cannot seem to get the function to output what I need.
This is what the data looks like:
Nest year Hatched Unhatched Pipped Rel_Eggs
0 1 1980 120.0 NaN NaN 120.0
1 2 1980 NaN NaN NaN NaN
2 3 1980 62.0 NaN NaN 117.0
3 4 1980 0.0 NaN NaN NaN
4 5 1980 0.0 NaN NaN NaN
5 6 1980 110.0 NaN NaN 114.0
Here is what I've already tried:
def new_success(data):
succ = []
for i in data.index:
nval = data.loc[i]['Hatched']/(data.loc[i]['Hatched'] + data.loc[i]['Unhatched'] + data.loc[i]['Pipped'])
if nval != np.inf and nval != np.nan:
succ.append(nval)
else:
val = data.loc[i]['Hatched']/data.loc[i]['Rel_Eggs']
succ.append(val)
return succ
The first formula is working; the second, conditional bit is not. Based on the above dataframe, for these first 6 values I should be be getting
[1.0, nan, 0.523, nan, nan, 0.965]
but I just get
[nan, nan, nan, nan, nan, nan]