'how can I change the code from Iterrows operation to data frame operations

sample code, how can I change the code from Iterrows to df operations. can you pls help. Iterrows is taking longer time

def renewed_penalty(row):
    row["Renewed_penalty_0"] = 0
    for i in range(0,37): 
        row["Renewed_penalty_"+ str(i)]=None
        for j in range(1,37):
            
            if(j<=row["term"] and row["Repay discount"] == 'Yes'):
                row["Renewed_penalty_"+ str(j)] = j* (row["Circuits_with_churn_"+str(j-1)]* row["avg_mrc"]
            elif row["Circuits_with_churn_"+str(j)] < (row["CCT_count"] * (1-row['Cease tolerance'])):
                row["Renewed_penalty_"+ str(j)] = row["avg_mrc"] * (1-row["Renewal_discount"])
            else:
                row["Renewed_penalty_"+ str(j)] =0
    return row

df1 = pd.Dataframe()
for index,row in df.iterrows():
    new_df = renewed_penalty(row)
    df1 = df1.append(new_df)


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source