Skip to content

Instantly share code, notes, and snippets.

@gautierdag
Created July 15, 2020 08:49
Show Gist options
  • Save gautierdag/65df852191069121b6a49fdf5aa19fed to your computer and use it in GitHub Desktop.
Save gautierdag/65df852191069121b6a49fdf5aa19fed to your computer and use it in GitHub Desktop.
Naive Date Compression
# set first (oldest balance as index entry)
new_indexes = [0]
# choose columns that are not the date column
cols = [d for d in df.columns if d != "date"]
# iterate over rows
for i, row in df.iterrows():
if i == 0: #skip 0 index since it has no previous balance
continue
# if not all values of the previous row/date is not equal to current - then we have new observation
if not (df.iloc[i-1][cols] ==row[cols]).all():
new_indexes.append(i)
#select new df based on indexes
compressed_df = df.loc[new_indexes].reset_index(drop=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment