Removing dynamic rows of a Dataframe
I have a named column maturity_dt
populated with objects datetime
in my dataframe df
and I am just trying to select only rows in column with maturity_dt
in August or February. Thus, I am trying to remove all rows that do not match those months dynamically using the code below. However, I am getting the error IndexError: index 109235 is out of bounds for axis 0 with size 44681
despite using it reset_index
, so I am wondering if there is another way to dynamically delete rows.
for (i, row) in df.iterrows():
dateold = datetime.datetime.strptime(row['maturity_dt'], '%Y-%m-%d %H:%M:%S.%f')
datenew = dateold.date()
date = str(datenew).split('.')[0]
h,m,s = re.split('-', date)
if m != 2 and m != 8: # If the month is not Feb or August
df.drop(df.index[i])
df = df.reset_index(drop=True)
thank
source to share