Issue
I am trying to do a simple for loop with if statements to get non-NA values, however, receiving an error. Could you help?
df = pd.DataFrame({'Platform ID' : [1,2,3,4], "Delivery Date" : [str(2009), float("nan"), float("nan"), float("nan")],
"Build Year" : [float("nan"),str(2009),float("nan"), float("nan")],
"In Service Date" : [float("nan"),float("nan"), str("14-11-2009"), float("nan")]})
df.dtypes
df
def delivery_year(delivery_year, build_year, service_year):
out = []
for i in range(0,len(delivery_year)):
if any(delivery_year.notna()):
out[i].append(delivery_year)
if any(delivery_year[i].isna() and build_year[i].notna()):
out[i].append(build_year)
elif any(build_year[i].isna()):
out[i].append(service_year.str.strip().str[-4:])
else:
out[i].append(float("nan"))
return out
df["Delivery Year"] = delivery_year(df["Delivery Date"], df["Build Year"], df["In Service Date"])
Error Message:
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
Solution
My guess would be to use np.where()
statement :
import numpy as np
df["Delivery Year"] = np.where(df['Delivery Date'].notna(),df['Delivery Date'],np.where(df['Build Year'].notna(),df['Build Year'],np.where(df['In Service Date'].notna(),df['In Service Date'].str.strip().str[-4:],float("nan"))))
Like this, your are going in the reverse side, when it's not null, take it, otherwise switch to another solution
np.where( IF, THEN, ELSE )
Answered By - Alex Germain
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.