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
import pandas as pd | |
col1 = 'event_date' | |
col_new = str(col1 + '_new') | |
col2 = 'received_date' | |
curr_year = 2020 | |
past_year = 2010 | |
# given a dataframe df with col1 and col2 as datetime columns |
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
# 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 |
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
# same iterrows() solution with change log | |
curr_year = 2020 | |
past_year = 2010 | |
change_log = [] | |
# loop through index and rows of df | |
for idx, row in df.iterrows(): | |
if row[col1].year > curr_year: | |
new_year = row[col2].year | |
# save reference data as tuple | |
records = tuple((row['case_id'] |
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)] |
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
# lambda with two conditional statements | |
df[col_new] = df.apply(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] | |
, axis=1) |
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
# replace col1 year with col2 year on a condition | |
# if does not meet condition, use the original col1 value | |
df[col_new] = df.apply(lambda x: x[col1].replace(year=x[col2].year) | |
if x[col1].year > curr_year else x[col1] | |
, axis=1) | |
# filter df where year is greater than current year | |
df = df[(df[col1].dt.year > curr_year)] | |
print(df[[col1, col_new, col2]].head(2)) | |
# output |
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
# copy col2 value into a new column | |
col_new = str(col1 + '_new') | |
df[col_new] = df.apply(lambda x: x[col2] , axis=1) | |
print(df[[col1, col_new, col2]].head(2)) | |
# output | |
""" | |
event_date event_date_new received_date | |
2011-01-05 2011-01-31 2011-01-31 |
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
# replace all values with the same thing | |
df[col1] = df[col1].apply(lambda x: x.replace(year=2109, month=1, day=1)) | |
print(df[col1].head(2)) | |
# output | |
""" | |
event_date | |
2109-01-01 | |
2109-01-01 |
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
data = {'dates': | |
['2012-05-04', | |
'2012-05-04', | |
'2012-06-04', | |
'2012-08-08'], | |
'types': | |
['a', | |
'a', | |
'z', | |
'z',], |
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
freq='M' | |
# or 'D' or 'Y' | |
df = df[['dates', 'types']].groupby([pd.Grouper(key='dates', freq=freq)]).agg('count').reset_index() | |
""" | |
dates count | |
2 2012-07-31 0 | |
1 2012-06-30 1 | |
3 2012-08-31 1 | |
0 2012-05-31 2 |
OlderNewer