Home>

I'm new to Python. Thank you.
I am currently thinking about creating shifts for my part-time job.
The list of shifts is shown in the following program.

Corresponding source code
#Set of part-time jobs
n_member = 3
member = pd.Series (f "member {m + 1}" for m in range (n_member))
#Set of dates
n_day = 1
day = pd.Series (f "day {d + 1}" for d in range (n_day))
#Time set
n_time = 9
time = pd.Series (f "{t + 12} o'clock" for t in range (n_time))
#Shift number
n_shiftnumber = 5
shiftnumber = pd.Series (f "shift {sn + 1}" for sn in range (n_shiftnumber))
#List of shifts
shift = [
    [0, 0, 0, 0, 0, 0, 0, 0, 0],
    [1, 1, 1, 1, 1, 1, 0, 0, 0],
    [0, 0, 0, 0, 1, 1, 1, 1, 1],
    [1, 1, 1, 0, 0, 1, 1, 1, 1],
    [1, 1, 1, 1, 0, 0, 1, 1, 1],
    ]
dfshift = pd.DataFrame (shift,
                      index = shiftnumber,
                      columns = time
                      )
shiftdaycount = [int (sum (x)>0) for x in shift]
#Desired work day
kibou = pd.DataFrame ([[1],
                      [1],
                      [1]], index = member, columns = day)


In shiftdaycount, 0 is absent and 1 is the work pattern. If the last desired work day is 0, shiftdaycount will be 0, and if it is 1, shiftdaycount will be 1 and the most suitable one will be selected according to the conditions. I am planning to create a program to do.
What I would like to ask here is that I replaced this directly typed shift pattern with a csv file as shown below, but an error occurred.

Corresponding source code
#Set of part-time jobs
n_member = 3
member = pd.Series (f "member {m + 1}" for m in range (n_member))
#Set of dates
n_day = 1
day = pd.Series (f "day {d + 1}" for d in range (n_day))
#Time set
n_time = 9
time = pd.Series (f "{t + 12} o'clock" for t in range (n_time))
#Shift number
n_shiftnumber = 5
shiftnumber = pd.Series (f "shift {sn + 1}" for sn in range (n_shiftnumber))
#List of shifts
shift = pd.read_csv ('shift.csv', header = None)
shift.index = shiftnumber
shift.columns = time
shiftdaycount = [int (sum (sdc)>0) for sdc in shift]
#Desired work day
kibou = pd.DataFrame ([[1],
                      [1],
                      [1]], index = member, columns = day)
shiftdaycount = [int (sum (sdc)>0) for sdc in shift]
TypeError: unsupported operand type (s) for +:'int' and'str'


I am sorry that this is a one-sided question, but I would appreciate it if you could answer it.

  • Answer # 1

    If you refer to it with shift, the header and index will be referenced, so try referencing it with'shift.values'.

    shiftdaycount = [int (sum (sdc)>0) for sdc in shift.values]

  • Answer # 2

    Why don't you output the contents of the variable shift and check it?
    I think that the shift in the changed code corresponds to the df shift in the original code.