Function Not Behaving As Intended - 2 Arguments, Check If Bool Is True
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!
Answer
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
####################################################################
Related Questions
- → What are the pluses/minuses of different ways to configure GPIOs on the Beaglebone Black?
- → Django, code inside <script> tag doesn't work in a template
- → React - Django webpack config with dynamic 'output'
- → GAE Python app - Does URL matter for SEO?
- → Put a Rendered Django Template in Json along with some other items
- → session disappears when request is sent from fetch
- → Python Shopify API output formatted datetime string in django template
- → Can't turn off Javascript using Selenium
- → WebDriver click() vs JavaScript click()
- → Shopify app: adding a new shipping address via webhook
- → Shopify + Python library: how to create new shipping address
- → shopify python api: how do add new assets to published theme?
- → Access 'HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT' with Python Shopify Module