我正在尝试编写一个脚本,用于检查经理号码是否与员工编号匹配。它将在列中继续,直到检查完所有数字。 完成后,它将打印一个匹配或不匹配的列表。
[In:]
import pandas as pd
#reading in csv file to Data Frame
employeeData = pd.read_csv("C:/Users/Desktop/EmployeeList.csv")
#creatig a Data Frame
dataF = pd.DataFrame(employeeData);
#empty list where instances of T/F will be stored
booleans = [];
#256 manager numbers + 1896 empty rows
managers = pd.Series(employeeData['Manager ID Number']
#Edit Forgot to include this line
condition = managers.equals(merge['Employee ID'])
#check each row of employee data. 2153 rows of Employee Numbers
for index, row in employeeData.iterrows():
#Check every single Manager number for a match
for index, row in managers.iteritems():
if condition:
booleans.append(True)
print("Something matched!")
else:
print("Didn't match!"
booleans.append(False)
#A length of all booleans is printed.
print(len(booleans))
[Out:] Actual
"Didn't match!" x 2153 times. (number of employees in list)
[Out:] Desired:
"Something matched!"
"Didn't match!"
"Something matched!"
"Something matched!"
"Didn't match!"
"Something matched!".... to line 2153
我的问题是索引计数似乎不会向下移动。它只会输出与第一个数字不匹配的数百次。我想将行位置向下移动,以便根据Manager列表检查所有员工编号。有些经理人有更多的员工,所以其他人所以我必须检查每一个人!(256)我很尴尬地说我已经坚持这个问题很长一段时间了。 python的新手,所以任何提示都会非常感激
答案 0 :(得分:0)
IIUC您需要使用Pandas Merge()
df_emp_mng= pd.merge(df_Emp,df_Mang,left_on='EMP ID',right_on='Manager ID')
print (df_emp_mng)
print 'Number of managers in Employee' ,len(df_emp_mng)
print 'Number of managers not in Employee' ,len(df_Emp)-len(df_emp_mng)
输入 - Emplyee数据
EMP ID name MID
0 123 E3 1
1 124 E1 1
2 125 E2 2
3 4 X4 5
输入 - 经理数据
Manager ID Manager name Dep
0 1 X1 C
1 2 X2 D
2 3 X3 E
3 4 X4 F
4 5 X5 F
输出
EMP ID name MID Manager ID Manager name Dep
0 4 X4 5 4 X4 F
Number of managers in Employee 1
Number of managers not in Employee 3