使用pandas在python中随机播放excel数据

时间:2019-12-30 18:12:59

标签: python pandas dataframe

这是我第一次尝试使用python进行编码。我正在尝试在这里解决问题。我在excel中有一些数据,我使用熊猫将其导入到数据框中,然后将其进一步转换为列表以执行某些操作。我根据员工的经验对他们进行了排序。之后,我得到以下数据:

index   emp_code    org_dept    new_dept    combo_dept  possibility grade   grade_marker    years_dept  flag<br/>
1   1028    D3  D2  D3D2    1   B+  2   6.4 0
2   1028    D3  D1  D3D1    1   B+  2   6.4 0
3   1039    D4  D2  D4D2    1   B+  2   6.4 0
4   1039    D4  D1  D4D1    1   B+  2   6.4 0
5   1007    D1  D3  D1D3    1   B+  2   6.3 0
6   1007    D1  D4  D1D4    1   B+  2   6.3 0
7   1010    D1  D3  D1D3    1   B   1   6.3 0
8   1010    D1  D4  D1D4    1   B   1   6.3 0
9   1012    D2  D3  D2D3    1   A+  4   6.3 0
10  1012    D2  D4  D2D4    1   A+  4   6.3 0
11  1017    D2  D3  D2D3    1   B+  2   6.3 0
12  1017    D2  D4  D2D4    1   B+  2   6.3 0
13  1034    D4  D2  D4D2    1   A   3   6.1 0
14  1034    D4  D1  D4D1    1   A   3   6.1 0
15  1001    D1  D3  D1D3    1   A+  4   5.5 0
16  1001    D1  D4  D1D4    1   A+  4   5.5 0
17  1016    D2  D3  D2D3    1   A   3   5.2 0
18  1016    D2  D4  D2D4    1   A   3   5.2 0
19  1033    D4  D2  D4D2    1   A   3   5.2 0
20  1033    D4  D1  D4D1    1   A   3   5.2 0
21  1022    D3  D2  D3D2    1   A+  4   5.1 0
22  1022    D3  D1  D3D1    1   A+  4   5.1 0

我的目标是在可能性为1的情况下将员工从其原来的部门转移到新部门。例如,如果某个雇员1028从D3移至D2,那么该雇员1012应该从D2移至D3,保持数量。然后我可以将这两个记录的标志设置为1。

我正在使用python 3.7

您能帮我提供代码吗?

1 个答案:

答案 0 :(得分:0)

因此,在这里,我们遍历df中的所有员工并将其移至新部门,然后尝试寻找其他员工并移动他以保持人数:

import pandas as pd


class Employee(object):
    def __init__(self, row):
        self.row = row
        self.moved = False
        self.org_dept = row['org_dept']


def employee_is_moved(emp_code):
    """ Return True if employee is already moved """
    for e_ in employees:
        if e_.row['emp_code'] == emp_code and e_.moved:
            return True


df = pd.read_csv('data.csv', delimiter=';')

employees = []
for index, row in df.iterrows():
    employees.append(Employee(row))

# Move employees to new departments
for e in employees:
    if all([e.row['possibility'] == 1,
            not e.moved,
            not employee_is_moved(e.row['emp_code'])]):
        # Move employee to new department
        print(f"Move employee index: {e.row['index']} from Dep. {e.row['org_dept']} to {e.row['new_dept']}")
        e.moved = True
        e.row['org_dept'] = e.row['new_dept']

        # Move another employee to maintain the number
        for e1 in employees:
            if all([e1.row['possibility'] == 1,
                    not e1.moved,
                    e1.row['org_dept'] == e.row['org_dept'],
                    e1.row['new_dept'] == e.org_dept,
                    not employee_is_moved(e1.row['emp_code'])]):
                print(f"Move employee (to maintain number) index: {e1.row['index']} from Dep. {e1.row['org_dept']} to {e1.row['new_dept']}")
                e1.moved = True
                e1.row['org_dept'] = e.row['new_dept']
                break

# Save result back to DF
df = pd.DataFrame([e.row for e in employees])

输出:

enter image description here