-2

Can somebody help me with the simpler way to write the following code? This code works, but it seems long, and when I want to repeat this it is time consuming. I am iterating over rows, but i'd like to run several .sum functions on each row during each iteration, and put that value in a new column.

for ind, row in tqdm(df.iterrows()):

    print("I'm Working ", end = '\r')
    df.loc[ind,"pre_fighter_2_sig_str_totals_total_A_all_fights_1"] = df[(df["B_fighter"] == row["B_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_2_sig_str_totals_total_A"].sum()
    df.loc[ind,"pre_fighter_2_sig_str_totals_total_B_all_fights_1"] = df[(df["B_fighter"] == row["B_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_2_sig_str_totals_total_B"].sum()
    df.loc[ind,"pre_fighter_1_total_str_totals_total_A_all_fights_1"] = df[(df["B_fighter"] == row["R_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_1_total_str_totals_total_A"].sum()
    df.loc[ind,"pre_fighter_1_total_str_totals_total_B_all_fights_1"] = df[(df["B_fighter"] == row["R_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_1_total_str_totals_total_B"].sum()
    df.loc[ind,"pre_fighter_2_total_str_totals_total_A_all_fights_1"] = df[(df["B_fighter"] == row["B_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_2_total_str_totals_total_A"].sum()
    df.loc[ind,"pre_fighter_2_total_str_totals_total_B_all_fights_1"] = df[(df["B_fighter"] == row["B_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_2_total_str_totals_total_B"].sum()
    df.loc[ind,"pre_fighter_1_td_totals_total_A_all_fights_1"] = df[(df["B_fighter"] == row["R_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_1_td_totals_total_A"].sum()
    df.loc[ind,"pre_fighter_1_td_totals_total_B_all_fights_1"] = df[(df["B_fighter"] == row["R_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_1_td_totals_total_B"].sum()
    df.loc[ind,"pre_fighter_2_td_totals_total_A_all_fights_1"] = df[(df["B_fighter"] == row["B_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_2_td_totals_total_A"].sum()
    df.loc[ind,"pre_fighter_2_td_totals_total_B_all_fights_1"] = df[(df["B_fighter"] == row["B_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_2_td_totals_total_B"].sum()
    df.loc[ind,"pre_fighter_1_sig_str_totals_round1_A_all_fights_1"] = df[(df["B_fighter"] == row["R_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_1_sig_str_totals_round1_A"].sum()
    df.loc[ind,"pre_fighter_1_sig_str_totals_round1_B_all_fights_1"] = df[(df["B_fighter"] == row["R_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_1_sig_str_totals_round1_B"].sum()
    df.loc[ind,"pre_fighter_2_sig_str_totals_round1_A_all_fights_1"] = df[(df["B_fighter"] == row["B_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_2_sig_str_totals_round1_A"].sum()
    df.loc[ind,"pre_fighter_2_sig_str_totals_round1_B_all_fights_1"] = df[(df["B_fighter"] == row["B_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_2_sig_str_totals_round1_B"].sum()
    df.loc[ind,"pre_fighter_1_total_str_totals_round1_A_all_fights_1"] = df[(df["B_fighter"] == row["R_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_1_total_str_totals_round1_A"].sum()
    df.loc[ind,"pre_fighter_1_total_str_totals_round1_B_all_fights_1"] = df[(df["B_fighter"] == row["R_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_1_total_str_totals_round1_B"].sum()
    df.loc[ind,"pre_fighter_2_total_str_totals_round1_A_all_fights_1"] = df[(df["B_fighter"] == row["B_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_2_total_str_totals_round1_A"].sum()
    df.loc[ind,"pre_fighter_2_total_str_totals_round1_B_all_fights_1"] = df[(df["B_fighter"] == row["B_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_2_total_str_totals_round1_B"].sum()
    df.loc[ind,"pre_fighter_1_td_totals_round1_A_all_fights_1"] = df[(df["B_fighter"] == row["R_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_1_td_totals_round1_A"].sum()
    df.loc[ind,"pre_fighter_1_td_totals_round1_B_all_fights_1"] = df[(df["B_fighter"] == row["R_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_1_td_totals_round1_B"].sum()
    df.loc[ind,"pre_fighter_2_td_totals_round1_A_all_fights_1"] = df[(df["B_fighter"] == row["B_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_2_td_totals_round1_A"].sum()
    df.loc[ind,"pre_fighter_2_td_totals_round1_B_all_fights_1"] = df[(df["B_fighter"] == row["B_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_2_td_totals_round1_B"].sum()
    df.loc[ind,"pre_fighter_1_sig_str_totals_round2_A_all_fights_1"] = df[(df["B_fighter"] == row["R_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_1_sig_str_totals_round2_A"].sum()
    df.loc[ind,"pre_fighter_1_sig_str_totals_round2_B_all_fights_1"] = df[(df["B_fighter"] == row["R_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_1_sig_str_totals_round2_B"].sum()
    df.loc[ind,"pre_fighter_2_sig_str_totals_round2_A_all_fights_1"] = df[(df["B_fighter"] == row["B_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_2_sig_str_totals_round2_A"].sum()
    df.loc[ind,"pre_fighter_2_sig_str_totals_round2_B_all_fights_1"] = df[(df["B_fighter"] == row["B_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_2_sig_str_totals_round2_B"].sum()
    df.loc[ind,"pre_fighter_1_total_str_totals_round2_A_all_fights_1"] = df[(df["B_fighter"] == row["R_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_1_total_str_totals_round2_A"].sum()
    df.loc[ind,"pre_fighter_1_total_str_totals_round2_B_all_fights_1"] = df[(df["B_fighter"] == row["R_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_1_total_str_totals_round2_B"].sum()
    df.loc[ind,"pre_fighter_2_total_str_totals_round2_A_all_fights_1"] = df[(df["B_fighter"] == row["B_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_2_total_str_totals_round2_A"].sum()
    df.loc[ind,"pre_fighter_2_total_str_totals_round2_B_all_fights_1"] = df[(df["B_fighter"] == row["B_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_2_total_str_totals_round2_B"].sum()
    df.loc[ind,"pre_fighter_1_td_totals_round2_A_all_fights_1"] = df[(df["B_fighter"] == row["R_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_1_td_totals_round2_A"].sum()
    df.loc[ind,"pre_fighter_1_td_totals_round2_B_all_fights_1"] = df[(df["B_fighter"] == row["R_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_1_td_totals_round2_B"].sum()
    df.loc[ind,"pre_fighter_2_td_totals_round2_A_all_fights_1"] = df[(df["B_fighter"] == row["B_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_2_td_totals_round2_A"].sum()
    df.loc[ind,"pre_fighter_2_td_totals_round2_B_all_fights_1"] = df[(df["B_fighter"] == row["B_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_2_td_totals_round2_B"].sum()
    df.loc[ind,"pre_fighter_1_sig_str_totals_round3_A_all_fights_1"] = df[(df["B_fighter"] == row["R_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_1_sig_str_totals_round3_A"].sum()
    df.loc[ind,"pre_fighter_1_sig_str_totals_round3_B_all_fights_1"] = df[(df["B_fighter"] == row["R_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_1_sig_str_totals_round3_B"].sum()
    df.loc[ind,"pre_fighter_2_sig_str_totals_round3_A_all_fights_1"] = df[(df["B_fighter"] == row["B_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_2_sig_str_totals_round3_A"].sum()
    df.loc[ind,"pre_fighter_2_sig_str_totals_round3_B_all_fights_1"] = df[(df["B_fighter"] == row["B_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_2_sig_str_totals_round3_B"].sum()
    df.loc[ind,"pre_fighter_1_total_str_totals_round3_A_all_fights_1"] = df[(df["B_fighter"] == row["R_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_1_total_str_totals_round3_A"].sum()
    df.loc[ind,"pre_fighter_1_total_str_totals_round3_B_all_fights_1"] = df[(df["B_fighter"] == row["R_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_1_total_str_totals_round3_B"].sum()
    df.loc[ind,"pre_fighter_2_total_str_totals_round3_A_all_fights_1"] = df[(df["B_fighter"] == row["B_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_2_total_str_totals_round3_A"].sum()
    df.loc[ind,"pre_fighter_2_total_str_totals_round3_B_all_fights_1"] = df[(df["B_fighter"] == row["B_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_2_total_str_totals_round3_B"].sum()
    df.loc[ind,"pre_fighter_1_td_totals_round3_A_all_fights_1"] = df[(df["B_fighter"] == row["R_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_1_td_totals_round3_A"].sum()
    df.loc[ind,"pre_fighter_1_td_totals_round3_B_all_fights_1"] = df[(df["B_fighter"] == row["R_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_1_td_totals_round3_B"].sum()
    df.loc[ind,"pre_fighter_2_td_totals_round3_A_all_fights_1"] = df[(df["B_fighter"] == row["B_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_2_td_totals_round3_A"].sum()
    df.loc[ind,"pre_fighter_2_td_totals_round3_B_all_fights_1"] = df[(df["B_fighter"] == row["B_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_2_td_totals_round3_B"].sum()
    df.loc[ind,"pre_fighter_1_sig_str_totals_round4_A_all_fights_1"] = df[(df["B_fighter"] == row["R_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_1_sig_str_totals_round4_A"].sum()
    df.loc[ind,"pre_fighter_1_sig_str_totals_round4_B_all_fights_1"] = df[(df["B_fighter"] == row["R_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_1_sig_str_totals_round4_B"].sum()
    df.loc[ind,"pre_fighter_2_sig_str_totals_round4_A_all_fights_1"] = df[(df["B_fighter"] == row["B_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_2_sig_str_totals_round4_A"].sum()
    df.loc[ind,"pre_fighter_2_sig_str_totals_round4_B_all_fights_1"] = df[(df["B_fighter"] == row["B_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_2_sig_str_totals_round4_B"].sum()
    df.loc[ind,"pre_fighter_1_total_str_totals_round4_A_all_fights_1"] = df[(df["B_fighter"] == row["R_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_1_total_str_totals_round4_A"].sum()
    df.loc[ind,"pre_fighter_1_total_str_totals_round4_B_all_fights_1"] = df[(df["B_fighter"] == row["R_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_1_total_str_totals_round4_B"].sum()
    df.loc[ind,"pre_fighter_2_total_str_totals_round4_A_all_fights_1"] = df[(df["B_fighter"] == row["B_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_2_total_str_totals_round4_A"].sum()
    df.loc[ind,"pre_fighter_2_total_str_totals_round4_B_all_fights_1"] = df[(df["B_fighter"] == row["B_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_2_total_str_totals_round4_B"].sum()
    df.loc[ind,"pre_fighter_1_td_totals_round4_A_all_fights_1"] = df[(df["B_fighter"] == row["R_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_1_td_totals_round4_A"].sum()
    df.loc[ind,"pre_fighter_1_td_totals_round4_B_all_fights_1"] = df[(df["B_fighter"] == row["R_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_1_td_totals_round4_B"].sum()
    df.loc[ind,"pre_fighter_2_td_totals_round4_A_all_fights_1"] = df[(df["B_fighter"] == row["B_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_2_td_totals_round4_A"].sum()
    df.loc[ind,"pre_fighter_2_td_totals_round4_B_all_fights_1"] = df[(df["B_fighter"] == row["B_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_2_td_totals_round4_B"].sum()
    df.loc[ind,"pre_fighter_1_sig_str_totals_round5_A_all_fights_1"] = df[(df["B_fighter"] == row["R_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_1_sig_str_totals_round5_A"].sum()
    df.loc[ind,"pre_fighter_1_sig_str_totals_round5_B_all_fights_1"] = df[(df["B_fighter"] == row["R_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_1_sig_str_totals_round5_B"].sum()
    df.loc[ind,"pre_fighter_2_sig_str_totals_round5_A_all_fights_1"] = df[(df["B_fighter"] == row["B_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_2_sig_str_totals_round5_A"].sum()
    df.loc[ind,"pre_fighter_2_sig_str_totals_round5_B_all_fights_1"] = df[(df["B_fighter"] == row["B_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_2_sig_str_totals_round5_B"].sum()
    df.loc[ind,"pre_fighter_1_total_str_totals_round5_A_all_fights_1"] = df[(df["B_fighter"] == row["R_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_1_total_str_totals_round5_A"].sum()
    df.loc[ind,"pre_fighter_1_total_str_totals_round5_B_all_fights_1"] = df[(df["B_fighter"] == row["R_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_1_total_str_totals_round5_B"].sum()
    df.loc[ind,"pre_fighter_2_total_str_totals_round5_A_all_fights_1"] = df[(df["B_fighter"] == row["B_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_2_total_str_totals_round5_A"].sum()
    df.loc[ind,"pre_fighter_2_total_str_totals_round5_B_all_fights_1"] = df[(df["B_fighter"] == row["B_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_2_total_str_totals_round5_B"].sum()
    df.loc[ind,"pre_fighter_1_td_totals_round5_A_all_fights_1"] = df[(df["B_fighter"] == row["R_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_1_td_totals_round5_A"].sum()
    df.loc[ind,"pre_fighter_1_td_totals_round5_B_all_fights_1"] = df[(df["B_fighter"] == row["R_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_1_td_totals_round5_B"].sum()
    df.loc[ind,"pre_fighter_2_td_totals_round5_A_all_fights_1"] = df[(df["B_fighter"] == row["B_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_2_td_totals_round5_A"].sum()
    df.loc[ind,"pre_fighter_2_td_totals_round5_B_all_fights_1"] = df[(df["B_fighter"] == row["B_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_2_td_totals_round5_B"].sum()
    df.loc[ind,"pre_fighter_1_sig_str_sig_str_totals_A_all_fights_1"] = df[(df["B_fighter"] == row["R_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_1_sig_str_sig_str_totals_A"].sum()
    df.loc[ind,"pre_fighter_1_sig_str_sig_str_totals_B_all_fights_1"] = df[(df["B_fighter"] == row["R_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_1_sig_str_sig_str_totals_B"].sum()
    df.loc[ind,"pre_fighter_2_sig_str_sig_str_totals_A_all_fights_1"] = df[(df["B_fighter"] == row["B_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_2_sig_str_sig_str_totals_A"].sum()
    df.loc[ind,"pre_fighter_2_sig_str_sig_str_totals_B_all_fights_1"] = df[(df["B_fighter"] == row["B_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_2_sig_str_sig_str_totals_B"].sum()
    df.loc[ind,"pre_fighter_1_sig_str_head_totals_A_all_fights_1"] = df[(df["B_fighter"] == row["R_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_1_sig_str_head_totals_A"].sum()
    df.loc[ind,"pre_fighter_1_sig_str_head_totals_B_all_fights_1"] = df[(df["B_fighter"] == row["R_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_1_sig_str_head_totals_B"].sum()
    df.loc[ind,"pre_fighter_2_sig_str_head_totals_A_all_fights_1"] = df[(df["B_fighter"] == row["B_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_2_sig_str_head_totals_A"].sum()
    df.loc[ind,"pre_fighter_2_sig_str_head_totals_B_all_fights_1"] = df[(df["B_fighter"] == row["B_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_2_sig_str_head_totals_B"].sum()
    df.loc[ind,"pre_fighter_1_sig_str_body_totals_A_all_fights_1"] = df[(df["B_fighter"] == row["R_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_1_sig_str_body_totals_A"].sum()
    df.loc[ind,"pre_fighter_1_sig_str_body_totals_B_all_fights_1"] = df[(df["B_fighter"] == row["R_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_1_sig_str_body_totals_B"].sum()
    df.loc[ind,"pre_fighter_2_sig_str_body_totals_A_all_fights_1"] = df[(df["B_fighter"] == row["B_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_2_sig_str_body_totals_A"].sum()
    df.loc[ind,"pre_fighter_2_sig_str_body_totals_B_all_fights_1"] = df[(df["B_fighter"] == row["B_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_2_sig_str_body_totals_B"].sum()
    df.loc[ind,"pre_fighter_1_sig_str_leg_totals_A_all_fights_1"] = df[(df["B_fighter"] == row["R_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_1_sig_str_leg_totals_A"].sum()
    df.loc[ind,"pre_fighter_1_sig_str_leg_totals_B_all_fights_1"] = df[(df["B_fighter"] == row["R_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_1_sig_str_leg_totals_B"].sum()
    df.loc[ind,"pre_fighter_2_sig_str_leg_totals_A_all_fights_1"] = df[(df["B_fighter"] == row["B_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_2_sig_str_leg_totals_A"].sum()
    df.loc[ind,"pre_fighter_2_sig_str_leg_totals_B_all_fights_1"] = df[(df["B_fighter"] == row["B_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_2_sig_str_leg_totals_B"].sum()
    df.loc[ind,"pre_fighter_1_sig_str_distance_totals_A_all_fights_1"] = df[(df["B_fighter"] == row["R_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_1_sig_str_distance_totals_A"].sum()
    df.loc[ind,"pre_fighter_1_sig_str_distance_totals_B_all_fights_1"] = df[(df["B_fighter"] == row["R_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_1_sig_str_distance_totals_B"].sum()
    df.loc[ind,"pre_fighter_2_sig_str_distance_totals_A_all_fights_1"] = df[(df["B_fighter"] == row["B_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_2_sig_str_distance_totals_A"].sum()
    df.loc[ind,"pre_fighter_2_sig_str_distance_totals_B_all_fights_1"] = df[(df["B_fighter"] == row["B_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_2_sig_str_distance_totals_B"].sum()
    df.loc[ind,"pre_fighter_1_sig_str_clinch_totals_A_all_fights_1"] = df[(df["B_fighter"] == row["R_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_1_sig_str_clinch_totals_A"].sum()
    df.loc[ind,"pre_fighter_1_sig_str_clinch_totals_B_all_fights_1"] = df[(df["B_fighter"] == row["R_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_1_sig_str_clinch_totals_B"].sum()
    df.loc[ind,"pre_fighter_2_sig_str_clinch_totals_A_all_fights_1"] = df[(df["B_fighter"] == row["B_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_2_sig_str_clinch_totals_A"].sum()
    df.loc[ind,"pre_fighter_2_sig_str_clinch_totals_B_all_fights_1"] = df[(df["B_fighter"] == row["B_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_2_sig_str_clinch_totals_B"].sum()
    df.loc[ind,"pre_fighter_1_sig_str_ground_totals_A_all_fights_1"] = df[(df["B_fighter"] == row["R_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_1_sig_str_ground_totals_A"].sum()
    df.loc[ind,"pre_fighter_1_sig_str_ground_totals_B_all_fights_1"] = df[(df["B_fighter"] == row["R_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_1_sig_str_ground_totals_B"].sum()
    df.loc[ind,"pre_fighter_2_sig_str_ground_totals_A_all_fights_1"] = df[(df["B_fighter"] == row["B_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_2_sig_str_ground_totals_A"].sum()
    df.loc[ind,"pre_fighter_2_sig_str_ground_totals_B_all_fights_1"] = df[(df["B_fighter"] == row["B_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_2_sig_str_ground_totals_B"].sum()
    df.loc[ind,"pre_fighter_1_sig_str_sig_str_round1_A_all_fights_1"] = df[(df["B_fighter"] == row["R_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_1_sig_str_sig_str_round1_A"].sum()
    df.loc[ind,"pre_fighter_1_sig_str_sig_str_round1_B_all_fights_1"] = df[(df["B_fighter"] == row["R_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_1_sig_str_sig_str_round1_B"].sum()
    df.loc[ind,"pre_fighter_2_sig_str_sig_str_round1_A_all_fights_1"] = df[(df["B_fighter"] == row["B_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_2_sig_str_sig_str_round1_A"].sum()
    df.loc[ind,"pre_fighter_2_sig_str_sig_str_round1_B_all_fights_1"] = df[(df["B_fighter"] == row["B_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_2_sig_str_sig_str_round1_B"].sum()
    df.loc[ind,"pre_fighter_1_sig_str_head_round1_A_all_fights_1"] = df[(df["B_fighter"] == row["R_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_1_sig_str_head_round1_A"].sum()
    df.loc[ind,"pre_fighter_1_sig_str_head_round1_B_all_fights_1"] = df[(df["B_fighter"] == row["R_fighter"]) & (df["Original Index"] < row["Original Index"])]["fighter_1_sig_str_head_round1_B"].sum()

KingPin
  • 3
  • 1
  • 2
    It would be helpful if you explained what you want to do. – timgeb May 12 '20 at 19:39
  • 1
    There is definitely a simpler way of doing this, but I think it would be easier for someone to help if you explained what you're trying to do. – Nicolas Gervais May 12 '20 at 19:39
  • 2
    Also check [how to ask pandas questions](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) – timgeb May 12 '20 at 19:40
  • I have rows of fight data. Each row contains the name of two fighters. One in the column "R_fighter" another in the column "B_fighter. Let's focus on "R_fighter". "R_fighter" has several columns with data from that fight. I want to iterate over the dataframe to create a new column that contains the sum of that column for all the previous rows containing "R_fighter". I want to do that over multiple columns for each row during each iteration. I can group all the columns where this needs to be done in sequential order if it helps. – KingPin May 12 '20 at 19:54

1 Answers1

0

Find some pattern and iterate key over an array

For example

for ind, row in tqdm(df.iterrows()):
    print("I'm Working ", end = '\r')
    keys = [
      "fighter_2_sig_str_totals_total_A",
      "fighter_2_sig_str_totals_total_B",
      ...
    ]
    b_fighter_rows = df["B_fighter"] == row["B_fighter"]
    less_than_original_rows = df["Original Index"] < row["Original Index"]
    for key in keys:
        df.loc[ind, key + "_all_fights_1"] = df[(b_fighter_rows & less_than_original_rows)][key].sum()
pepe_witch
  • 161
  • 5