-1

I have this which works, but I would like to put this into a list comprehension to save computing power

emp_id=df_hours["EMPLOYEE"]
emp_list=df_item["EMPLOYEE"]

##############
#loop through and get values for the day for hours worked per server, and add them to building df
#############

hours_worked=df_hours["HOURS"]
emp_found=[]
 
for x in range(len(emp_list)):
    for i in range(len(emp_id)):
        if emp_list[x] == emp_id[i]:
            emp_found.append(hours_worked[i])

inex=0
last=len(emp_list)
hours_wored=emp_found[inex:last]
df_item['HRS']=hour_worked
Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158

2 Answers2

0

I believe this is what you're looking for

emp_found = [hours_worked[i] for i in range(len(emp_id)) for x in range(len(emp_list)) if emp_list[x] == emp_id[i]]
Jacobjanak
  • 307
  • 1
  • 2
  • 10
0

Here's a shorter, more elegant way:

print(list(set(df_hours["EMPLOYEE"]).intersection(set(df_item["EMPLOYEE"]))))

Since what you are looking for is getting a list with all common values, you can transform each column to a set, and get the intersection. Then you can convert back the result to a list.

I find this syntax shorter and you might say; more pythonic.

snatchysquid
  • 1,283
  • 9
  • 24