Created
December 30, 2020 22:01
-
-
Save justinhchae/74d7bcabd5937797b8ebeaac5821ff44 to your computer and use it in GitHub Desktop.
impute with iteration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# iterate through a DataFrame with iterrows() | |
curr_year = 2020 | |
past_year = 2010 | |
# loop through index and rows of df | |
for idx, row in df.iterrows(): | |
# compare years as integers | |
if row[col1].year > curr_year: | |
# return received_date's year as integer | |
new_year = row[col2].year | |
# for the row index and column, | |
df.loc[idx, col1] = row[col1].replace(year=new_year) | |
# do the same for past year | |
if row[col1].year < past_year: | |
# return received_date's year as integer | |
new_year = row[col2].year | |
# for the row index and column, | |
df.loc[idx, col1] = row[col1].replace(year=new_year) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment