I am trying to run conditional statements when iterating through pandas df rows and it results with a very slow code. For example:
for i, row in df.iterrows(): # transform date column if len(df.loc[i, 'date']) == 7: df.loc[i, 'date'] = '0' + df.loc[i, 'date']
The df is only about 40k rows long and it’s very slow, as this is only one of the statements I am trying to incorporate with this loop. Can you help with a faster way to do such a loop?
Thank you!
Advertisement
Answer
Locate the relevant rows and modify them:
df.loc[df["date"].str.len() == 7, "date"] = "0" + df.loc[df["date"].str.len()== 7, "date"]