I have a dataframe that looks something like:
Brand Price
0 toronto 16-Aug-16
1 quebec 18-May-17
2 brampton 18-May-17
3 toronto 31-Dec-97
My code at the moment is as follows:
df["YEAR_TORONTO"] = np.nan
df["YEAR_TORONTO"] = np.where(df["Brand"] == "toronto", df["Price"], np.nan)
df["YEAR_TORONTO"] = df["YEAR_TORONTO"].str[-2:]
df["YEAR_TORONTO"] = np.where(isinstance(df["YEAR_TORONTO"], str) and df["YEAR_TORONTO"].astype(int) >= 20, "19" + df["YEAR_TORONTO"], "20" + df["YEAR_TORONTO"])
df["YEAR_TORONTO"] = df["YEAR_TORONTO"].fillna(0).astype(int).astype(object).where(df["YEAR_TORONTO"].notnull())
This gives me a result that looks like:
Brand Price YEAR_TORONTO
0 toronto 16-Aug-16 2016
1 quebec 18-May-17 NaN
2 brampton 18-May-17 NaN
3 toronto 31-Dec-97 2097
I'm aware that the 2097 instead of the 1997 is due to the and condition in my logic, but I'm not sure how to fix this due to the NaN values interfering and giving errors. Any help correcting this code would be greatly beneficial, thank you.