我有下面的数据集。我想根据emp_worked月份添加每个emp的薪水。对于ex如下面的数据集emp所示,名称为'aaa'的工作了4个月,所以我想将jan列添加到Apr并将其存储在Total_ sal中列。
inputValue
答案 0 :(得分:0)
# Get the index where jan starts
months_index_start = df.columns.get_loc("jan")
# Calculate the total salary for each row according to the months_worked column
df["total_sal"] = df.apply(lambda x : x[months_index_start : months_index_start + x["months_worked"]].sum(), axis = 1)