如何在Pandas DataFrame中执行列的条件添加?

时间:2018-09-08 12:17:47

标签: python python-3.x pandas dataframe

我有下面的数据集。我想根据emp_worked月份添加每个emp的薪水。对于ex如下面的数据集emp所示,名称为'aaa'的工作了4个月,所以我想将jan列添加到Apr并将其存储在Total_ sal中列。

inputValue

1 个答案:

答案 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)