0

I have a dataframe like this,

ID   F/P     FTE Actual     FTE Calculated
1    F       100              100
2    F       110              110
3    P       90               100
4    P       90               90

I want to generate the result column

if FTE Actual is not equal to FTE Calculated then result is FALSE and we don't check any further.

if true, then we look into F/P column, if F/P column has a value of F, then the FTE_Calculated should be equal to 100.

if true, then we look into F/P column, if F/P column has a value of P, then the FTE_Calculated should be less than 100.

ID   F/P     FTE Actual     FTE Calculated   Expected Result
1    F       100              100                 True
2    F       110              110                 False (as it is more than 100)
3    P       90               100                 False
4    P       90               90                  True

2 Answers2

6

If many conditions the best readable is create each one to separate row and then chain by | with & by your logic:

m1 = df['FTE Actual'] == df['FTE Calculated']
m2 = df['F/P'] == 'F'
m3 = df['FTE Calculated'] == 100
m4 = df['F/P'] == 'P'
m5 = df['FTE Calculated'] < 100

df['Expected Result'] = m1 & ((m4 & m5) | (m2 & m3))
print (df)
   ID F/P  FTE Actual  FTE Calculated  Expected Result
0   1   F         100             100             True
1   2   F         110             110            False
2   3   P          90             100            False
3   4   P          90              90             True
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
0

Jezrael's solution's the way to go. If you want to find out how to do boolean indexing yourself the following can be helpful:

Logical operators for boolean indexing in Pandas

inneb
  • 1,060
  • 1
  • 9
  • 20