I'm iterating through rows of a dataframe to extract values as follows but what I receive is always a float value and I'm not able to convert to int for both result["YEAR_TORONTO"]
and result["YEAR_TORONTO2"]
for i in range(0, len(result)):
if result["SOURCE_DATASET"].iloc[i] == "toronto":
result["YEAR_TORONTO"].iloc[i] = pd.to_datetime(result["START_DATE"].iloc[i]).year
result["YEAR_TORONTO"].iloc[i].astype(int) if not np.isnan(result["YEAR_TORONTO"].iloc[i]) else np.nan
result["YEAR_TORONTO2"].iloc[i] = result["YEAR_TORONTO"].iloc[i]
Any idea as for why this could be? Tried multiple approaches including pd.to_numeric
and round()
but no luck despite the method
Interestingly enough, when I output
result["YEAR_TORONTO"].iloc[1].astype(int) if not np.isnan(result["YEAR_TORONTO"].iloc[i]) else np.nan
,
I get 2016
as an int, but once I output the entire dataframe by calling result
, I still get 2016.0
as a float
Sample Data (Input):
SOURCE_DATASET START_DATE
0 brampton 06-04-16
1 toronto 06-04-16
2 brampton 06-04-16
3 toronto 06-04-99
Sample Data (Output):
SOURCE_DATASET START_DATE YEAR_TORONTO YEAR_TORONTO2
0 brampton 06-04-16 NaN NaN
1 toronto 06-04-16 2016.0 2016.0
2 brampton 06-04-16 NaN NaN
3 toronto 06-04-99 1999.0 1999.0
Just tried with np.where
as well and getting the same result.