Skip to content

Instantly share code, notes, and snippets.

@justinhchae
Last active January 1, 2021 02:17
Show Gist options
  • Save justinhchae/756a407fe71b29e6af8fb43dff2e564f to your computer and use it in GitHub Desktop.
Save justinhchae/756a407fe71b29e6af8fb43dff2e564f to your computer and use it in GitHub Desktop.
impute dates with apply and lambdas with change log
# store the lambda function as an object
impute = lambda x: x[col1].replace(year=x[col2].year) if x[col1].year > curr_year \
else x[col1].replace(year=x[col2].year) if x[col1].year < past_year \
else x[col1]
# simplify the code later by calling impute
df[col_new] = df.apply(impute, axis=1)
# a new dataframe called change_log
change_log = df[(df[col1].dt.year > curr_year)]
# write to csv
change_log.to_csv('change_log.csv')
# replace col1 with new_col values
df[col1] = df[col_new]
# drop the temporary new_col
df.drop(columns=col_new, inplace=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment