由不是索引变量的列求和(Python)

2024-10-03 06:21:36 发布

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

这里是Python新手(背景主要是SAS)。你知道吗

我尝试按不是索引变量的列求和(在下面的示例中,索引变量是'department',我尝试按'employee\u fixed'求和)。我不能将它设为索引变量,因为索引变量正被用作for循环的一部分。下面的代码应该说明这一点。你知道吗

#Creating dataset of departments you want to keep in your dataset
   #Setting df to only include departments specified
    cc = ['Furniture','Food','Clothing']
    for index in range(len(cc)): 
    df3_cc = df[df['department'].isin([cc[index]])]
    #set the department as the index variable so you can aggregate 
    df3_cc = df3_cc.set_index('department')
    df3_cc
    #Creating dataset of people who are NOT approved department
     #Setting df to only include the condition specified in "notapprov"
    notapprov = ['NO']
    df3_cc = df3_cc[df3_cc['appr_list_chc'].isin(notapprov)]
    df3_cc
    #drop unnecessary columns from dataframe
    df3_cc = df3_cc.drop(['fisc_yr_per'], axis=1)
    # sum up the hours based on the indexed departments
    # for those NOT approved to work that department and charging anyway
    # >40hrs in the latest period
    df3_cc = df3_cc[df3_cc['hrs_per'] >= 40].sum(level='employee_fixed') 
    #output to CSV
    df3_cc.to_csv(r"C:\Users\etc\table3_"+cc[index]+".csv")

最终结果应该是“cc”中每个项目的单独CSV,每个员工在未授权在该部门工作的每个部门工作的总小时数(在“employee\u fixed”中)(仅包括当前期间工作超过40小时的员工)。你知道吗

样本输入: 部门员工固定审批表 约翰45号家具 家具雅各布50号 食物杰基是100 食物杰里米75号 食物吉姆10号 乔纳斯200号服装 衣服杰瑞是的10

输出: 表3_家具.csv 部门员工固定审批表 约翰45号家具 家具雅各布50号

表3_食品.csv 部门员工固定审批表 食物杰里米75号

表3_食品.csv 部门员工固定审批表 乔纳斯200号服装

谢谢!你知道吗

编辑:找到答案了! df3_cc=df3_cc[df3_cc['hrs_per']>;=40].sum(level='employee_fixed') 成为 df3_cc=df3_cc[df3_cc['每小时]>;=40]


Tags: csvthetoindfindex员工employee