0

THis is a building off Categorical assignments using pd.apply based on multiple conditions on Pandas Columns

i have a dataframe that contains Item Id numbers with multiple tasks and completion dates for those tasks. I am trying to assign categories based on task completions or in-completions in a separate column

my data frame looks like this:

Item ID     Task 1 Comp Date  Task 2 Comp Date  Task 3 Comp Date Solution
12781463    NaT               NaT               NaT              Solution X
10547725    6/6/2019          7/30/2019         8/1/2019         Solution Y
12847251    5/31/2019         6/12/2019         NaT              Solution Y
12734403    5/31/2019         NaT               NAT              Solution Y

to test my approach to my challenge i took a subset of my data set and wrote a portion of the function that will be used with pd.apply(). below is some sample code for my .apply() function

def gating(row):
    if pd.notnull(row['Task 3 Comp Date']):
        return "Complete"
    elif (row['Solution'] == 'Solution X' & pd.isnull(row['Task 3 Comp Date'])):
        return "Pending Solution X"



df['Gating'] = df.apply(gating, axis = 1)

I expected to see Pending Solution X for Item ID 12781463 but got the error message below. Seems like .apply() doesnt play well with what i am trying to do

("unsupported operand type(s) for &: 'str' and 'bool'", 'occurred at index 9')

anky
  • 74,114
  • 11
  • 41
  • 70
  • 1
    This is just like np.select problem, you do not need apply or self-define function – BENY Aug 02 '19 at 15:44
  • Agreed, please look at : https://stackoverflow.com/questions/19913659/pandas-conditional-creation-of-a-series-dataframe-column – anky Aug 02 '19 at 15:45

0 Answers0