# Function Not Behaving As Intended - 2 Arguments, Check If Bool Is True

## 08 August 2019 - 1 answer

I need a function to check, row by row, if value is True or False

(df['is_unique_ID'] contains True or False values). If True, it needs to return the (numerical) value of another column df['etp']

``````def get_etp(self,per_id_u,etp):
if per_id_u is True:
return etp
else:
return "test"

df['new_col'] = df.apply(get_etp,args= (df['is_unique_ID'],df['etp']),axis=1)

``````

unfortunately, that returns a column with only 'test' as values, while I know that df['is_unique_ID'] contains about 4000 True and 250 False

Btw, this is step 1 of a more complicated function, so I would appreciate solutions that keep using the def function and apply , as I will be adding more arguments and elifs later on:

``````(not python code)
The full function will create new_col =
1)  if is_unique_ID is True --> = ETP
2)  if is_unique_ID is False -->
2.1) if col_1 is True --> = ETP_2
2.2) if col_1 is False --> = ETP_3

``````

Many thanks!

I am not quite sure if i got your question right, however i'll give it a shot. Here is what you can try

``````import pandas as pd
import numpy as np

df1 = pd.DataFrame({'is unique': [False, True, True, False] , 'etp':[2, 5, 3, 4]},  index=None)

print(df1)
#    is unique  etp
0      False    2
1       True    5
2       True    3
3      False    4

print(df1[df1['is unique']])
#    is unique  etp
1       True    5
2       True    3

# If your intention is to create a new column, you can do so by doing the following:
df1['new_col'] = df1['etp'][df1['is unique']]
print(df1)

#   is unique  etp  new_col
0      False    2      NaN
1       True    5      5.0
2       True    3      3.0
3      False    4      NaN
``````

Edit instead of applying a function across the row, what you can do is to just simply query and create a new row of column.

``````import pandas as pd
import numpy as np

df1 = pd.DataFrame({'is unique': [True, True, True, False] ,'col_1': [True, False, True, False] , 'etp':[2, 5, 3, 4],'etp2':[1, 0, 1, 0],'etp3':[1, 0, 1, 0]})

print(df1)
########################################
is unique  col_1  etp  etp2  etp3
0       True   True    2     1     1
1       True  False    5     0     0
2       True   True    3     1     1
3      False  False    4     0     0
########################################

df1['new_col'] = df1['etp'][df1['is unique']]
df1['new_col1'] = df1['etp2'][df1['col_1']]
df1['new_col2'] = df1['etp3'][ ~df1['col_1']]
print(df1)

####################################################################
is unique  col_1  etp  etp2  etp3  new_col  new_col1  new_col2
0       True   True    2     1     1      2.0       1.0       NaN
1       True  False    5     0     0      5.0       NaN       0.0
2       True   True    3     1     1      3.0       1.0       NaN
3      False  False    4     0     0      NaN       NaN       0.0
####################################################################
``````