-1

I am looking to be able to loop through line by line of my dataframe, checking certain criteria, (if a staff member is available at a certain time etc.) and adding them to a list if they are available. To do this I plan on looping through each row of my dataframe until it's gone through them all. Having some trouble currently, any help is appreciated. In the example, I just want it to simply count the rows, just so I know the looping through actually works. This is what I am trying to replicate in the end. If anyone has knows how they can help achieve the end goal I will appreciate that greatly.

ps. I am not too experienced so sorry if this is a silly question, don't be too harsh.

def staffEarlyShift(staff):
    x = 1
    staff_shift = []
    for i in range(len(staff)):
        print (x)
        x = x + 1

staffEarlyShift(staff_df)




dataframe

    staffID                   days_can_work time_can_work     role
0         1  [mon,tue,wed,thur,fri,sat,sun]         (9,7)  waitron
1         2  [mon,tue,wed,thur,fri,sat,sun]         (9,7)  waitron
2         3  [mon,tue,wed,thur,fri,sat,sun]         (9,7)  waitron
3         4  [mon,tue,wed,thur,fri,sat,sun]         (9,7)  waitron
4         5  [mon,tue,wed,thur,fri,sat,sun]         (9,7)  waitron
5         6  [mon,tue,wed,thur,fri,sat,sun]         (9,7)     chef
6         7  [mon,tue,wed,thur,fri,sat,sun]         (9,7)  waitron
7         8  [mon,tue,wed,thur,fri,sat,sun]         (9,7)  waitron
8         9  [mon,tue,wed,thur,fri,sat,sun]         (9,7)     chef
9        10  [mon,tue,wed,thur,fri,sat,sun]         (9,7)  waitron
10       11  [mon,tue,wed,thur,fri,sat,sun]         (9,7)  waitron
11       12  [mon,tue,wed,thur,fri,sat,sun]         (9,7)  waitron
12       13  [mon,tue,wed,thur,fri,sat,sun]         (9,7)  waitron
13       14  [mon,tue,wed,thur,fri,sat,sun]         (9,7)     chef
14       15  [mon,tue,wed,thur,fri,sat,sun]         (9,7)     chef
15       16  [mon,tue,wed,thur,fri,sat,sun]         (9,7)     chef
16       17  [mon,tue,wed,thur,fri,sat,sun]         (9,7)  waitron
17       18  [mon,tue,wed,thur,fri,sat,sun]         (9,7)  waitron
18       19  [mon,tue,wed,thur,fri,sat,sun]         (9,7)  waitron
  • 3
    What information did you find when you searched for `pandas iterate through dataframe rows` or something similar? Also, what does your code have to do with anything? It simply prints out numbers in a loop... – MattDMo Jan 05 '22 at 20:53
  • 2
    Does [this](https://stackoverflow.com/a/55557758/5327068) answer your question? – Derek O Jan 05 '22 at 20:57

1 Answers1

0

I don't know what kind of output you want, but you can use if statement in a loops and use .iloc for take dataframes by row like this:

staff_available = []
for i in range(len(df)):
    if len(df['DayWork'].iloc[i]) == 7: # Condition Is Can Work Everyday
        staff_available.append(df.iloc[i].tolist())
df2 = pd.DataFrame(staff_available,columns = ['StaffID','DayWork','TimeWork','Role'])

And result like this: enter image description here

You can change if statement by what kind of output you want (for example use time work)

Wahyu Hadinoto
  • 198
  • 1
  • 10