简体   繁体   中英

shuffle excel data in python using pandas

It is my first time trying to code in python. I'm trying to solve a problem here. I've some data in excel which I import using pandas into dataframe and further convert it into list to perform certain operations. I've sorted the employees based on their experience. After which I get below data :

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

My goal is to move employees from their original department to new department if the possibility is 1. Now here's the thing if for example, an employee-1028 moves from D3 to D2 then employee-1012 should move from D2 to D3, to maintain the number. Then I can set flag for these 2 record as 1.

I'm using python 3.7

Can you please help me with the code?

So here we iterate over all employees in the df and move them to the new department, then try to find other employee and move him to maintain the number:

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])

Output:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM