首页 > 解决方案 > 使用熊猫在python中随机播放excel数据

问题描述

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

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,以保持数字。然后我可以将这 2 条记录的标志设置为 1。

我正在使用 python 3.7

你能帮我写代码吗?

标签: pythonpandasdataframe

解决方案


所以这里我们遍历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])

输出:

在此处输入图像描述


推荐阅读