Ad
Replacing The Value In A Column Based On A Single Vectorized Condition
I tried the following code with multiple conditions and worked perfectly, somehow whatever I am trying with a single condition it won't work. It will simply return the values "1" & "SD" for every row no matter if the value of Col_A is True. Please let me know any suggestions, the weird part is that I am not getting any error whatsoever.
condition = [
df1['Col_A'] == 'TRUE'
]
result1 = ['1']
result2 = ['SD']
df1['Col_B'] = np.select(condition, result1, 'NA')
df1['Col_C'] = np.select(condition, result2, 'NA')
df1```
Input
Col_A Col_B Col_C
0 TRUE NA NA
1 FALSE 1001 Valid
2 TRUE NA NA
3 TRUE NA NA
Output
Col_A Col_B Col_C
0 TRUE NA NA
1 FALSE NA NA
2 TRUE NA NA
3 TRUE NA NA
Desired Output
Col_A Col_B Col_C
0 TRUE 1 SD
1 FALSE 1001 Valid
2 TRUE 1 SD
3 TRUE 1 SD
Ad
Answer
You could use a simple loc
assignment with a condition:
string "TRUE"
:
df.loc[df['Col_A'].eq('TRUE'), ['Col_B', 'Col_C']] = [1, 'SD']
boolean True
:
df.loc[df['Col_A'], ['Col_B', 'Col_C']] = [1, 'SD']
output:
Col_A Col_B Col_C
0 TRUE 1.0 SD
1 FALSE 1001.0 Valid
2 TRUE 1.0 SD
3 TRUE 1.0 SD
Ad
source: stackoverflow.com
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
Ad