Skip to content
Advertisement

Fastest way to use if/else statements when looping through dataframe with pandas

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"]
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement