【问题标题】:More efficient way to iterate over rows and columns?迭代行和列的更有效方法?
【发布时间】:2023-03-22 03:57:01
【问题描述】:

有人可以帮助我用更简单的方法编写以下代码吗?这段代码有效,但看起来很长,当我想重复这个时,它很耗时。我正在迭代行,但我想在每次迭代期间在每行上运行几个 .sum 函数,并将该值放在一个新列中。

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()

【问题讨论】:

  • 如果您解释了您想要做什么,将会很有帮助。
  • 肯定有一种更简单的方法可以做到这一点,但我认为如果你解释了你想要做什么,别人会更容易提供帮助。
  • 我有一排战斗数据。每行包含两名战士的名字。一个在“R_fighter”列中,另一个在“B_fighter”列中。让我们关注“R_fighter”。“R_fighter”有几列包含那场战斗的数据。我想遍历数据框以创建一个包含总和的新列包含“R_fighter”的所有先前行的该列。我想在每次迭代期间对每行的多个列执行此操作。如果有帮助,我可以将需要按顺序完成的所有列分组。

标签: python pandas loops


【解决方案1】:

找到一些模式并在数组上迭代键

例如

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()

【讨论】:

    猜你喜欢
    • 2012-04-11
    • 1970-01-01
    • 2022-10-19
    • 1970-01-01
    • 1970-01-01
    • 2022-01-13
    • 2017-05-08
    • 1970-01-01
    • 2016-08-21
    相关资源
    最近更新 更多