如何在Pandas DataFrame中有条件地添加列?

2024-09-29 05:17:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我有下面的数据集。我想加上每个雇员的工资,这取决于雇员的月数_工作。为了如下面的数据集所示,名为“aaa”的emp工作了4个月,所以我想添加jan到Apr列,并将其存储在Total_usal列中。在

`import pandas as pd`

    data=pd.read_csv("C:\\Users\\hp\\Desktop\\py\\p1.txt",delimiter=',')
    df=pd.DataFrame(data)
    print(df)



  emp_id emp_name  months_worked  total_sal   jan  feb      mar  apr  may  \
0       1      aaa              4        NaN  2000    1      2.0    3  4.0   
1       2      bbb              3        NaN     1    2      NaN    4  5.0   
2       3      bbb              7        NaN     1    2  34343.0    4  NaN   
3       4      bbb             12        NaN     1    2  33434.0    4  5.0   

      jun  jul      aug   sep   oct      nov   dec  
0  5555.0  NaN  74343.0     8     9     10.0   NaN  
1   643.0  7.0      NaN  9343    10  13431.0  12.0  
2  6343.0  7.0      NaN     9  1043     11.0  12.0  
3     NaN  7.0      8.0     9     1      NaN  12.0 

Tags: 数据importpandasdfdatananaprjan
2条回答
# 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)
df['Total_sal'] = df[df.columns[df.columns.get_loc('jan'):df.columns.get_loc('apr')]].sum(axis=1)

输出:

^{pr2}$

相关问题 更多 >