Last active
January 1, 2021 02:17
-
-
Save justinhchae/756a407fe71b29e6af8fb43dff2e564f to your computer and use it in GitHub Desktop.
impute dates with apply and lambdas with change log
This file contains hidden or 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
# 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