Skip to content

Instantly share code, notes, and snippets.

@justinhchae
Created December 30, 2020 22:01
Show Gist options
  • Save justinhchae/74d7bcabd5937797b8ebeaac5821ff44 to your computer and use it in GitHub Desktop.
Save justinhchae/74d7bcabd5937797b8ebeaac5821ff44 to your computer and use it in GitHub Desktop.
impute with iteration
# 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