np.logical_and with three tests
I am trying to update a column in my dataframe based on the testing and results of the values within three columns (3 tests).
Some sample code:
df_test = pd.DataFrame([('?',2.0,1,0,0,0), (None,2.0,1,0,0,0),
(None,2.0,0,0,0,0),(None,2.0,0,1,0,0),
('?',2.0,0,0,0,0)], columns=['a','b','c','d','e','f'])
df_test.head()
When I try the following df_test['g'] = np.where(np.logical_and(df_test['a'] != 'None', df_test['c'] == 0, df_test['d'] == 0), True, False).astype(int)
I receive the error TypeError: return arrays must be of ArrayType
So I try the following:
df_test = pd.DataFrame([('?',2.0,1,0,0,0), (None,2.0,1,0,0,0),
(None,2.0,0,0,0,0),(None,2.0,0,1,0,0),
('?',2.0,0,0,0,0)], columns=['a','b','c','d','e','f'])
df_test['g'] = np.where(np.logical_and(df_test['a'] != None,
np.logical_and(df_test['c'] == 0,
df_test['d'] == 0)),
True, False).astype(int)
df_test.head()
And on row 2, where I would expect to see a 0 I see a 1 while row 4 appears correct. Test 1 (a), should be False while the second (c) and third (d) tests should be True, True. False == True == True is False.
a b c d e f g
0 ? 2.0 1 0 0 0 0
1 None 2.0 1 0 0 0 0
2 None 2.0 0 0 0 0 1
3 None 2.0 0 1 0 0 0
4 ? 2.0 0 0 0 0 1
I need a way to evaluate 3 tests and return true or false as an int.
python pandas numpy
add a comment |
I am trying to update a column in my dataframe based on the testing and results of the values within three columns (3 tests).
Some sample code:
df_test = pd.DataFrame([('?',2.0,1,0,0,0), (None,2.0,1,0,0,0),
(None,2.0,0,0,0,0),(None,2.0,0,1,0,0),
('?',2.0,0,0,0,0)], columns=['a','b','c','d','e','f'])
df_test.head()
When I try the following df_test['g'] = np.where(np.logical_and(df_test['a'] != 'None', df_test['c'] == 0, df_test['d'] == 0), True, False).astype(int)
I receive the error TypeError: return arrays must be of ArrayType
So I try the following:
df_test = pd.DataFrame([('?',2.0,1,0,0,0), (None,2.0,1,0,0,0),
(None,2.0,0,0,0,0),(None,2.0,0,1,0,0),
('?',2.0,0,0,0,0)], columns=['a','b','c','d','e','f'])
df_test['g'] = np.where(np.logical_and(df_test['a'] != None,
np.logical_and(df_test['c'] == 0,
df_test['d'] == 0)),
True, False).astype(int)
df_test.head()
And on row 2, where I would expect to see a 0 I see a 1 while row 4 appears correct. Test 1 (a), should be False while the second (c) and third (d) tests should be True, True. False == True == True is False.
a b c d e f g
0 ? 2.0 1 0 0 0 0
1 None 2.0 1 0 0 0 0
2 None 2.0 0 0 0 0 1
3 None 2.0 0 1 0 0 0
4 ? 2.0 0 0 0 0 1
I need a way to evaluate 3 tests and return true or false as an int.
python pandas numpy
add a comment |
I am trying to update a column in my dataframe based on the testing and results of the values within three columns (3 tests).
Some sample code:
df_test = pd.DataFrame([('?',2.0,1,0,0,0), (None,2.0,1,0,0,0),
(None,2.0,0,0,0,0),(None,2.0,0,1,0,0),
('?',2.0,0,0,0,0)], columns=['a','b','c','d','e','f'])
df_test.head()
When I try the following df_test['g'] = np.where(np.logical_and(df_test['a'] != 'None', df_test['c'] == 0, df_test['d'] == 0), True, False).astype(int)
I receive the error TypeError: return arrays must be of ArrayType
So I try the following:
df_test = pd.DataFrame([('?',2.0,1,0,0,0), (None,2.0,1,0,0,0),
(None,2.0,0,0,0,0),(None,2.0,0,1,0,0),
('?',2.0,0,0,0,0)], columns=['a','b','c','d','e','f'])
df_test['g'] = np.where(np.logical_and(df_test['a'] != None,
np.logical_and(df_test['c'] == 0,
df_test['d'] == 0)),
True, False).astype(int)
df_test.head()
And on row 2, where I would expect to see a 0 I see a 1 while row 4 appears correct. Test 1 (a), should be False while the second (c) and third (d) tests should be True, True. False == True == True is False.
a b c d e f g
0 ? 2.0 1 0 0 0 0
1 None 2.0 1 0 0 0 0
2 None 2.0 0 0 0 0 1
3 None 2.0 0 1 0 0 0
4 ? 2.0 0 0 0 0 1
I need a way to evaluate 3 tests and return true or false as an int.
python pandas numpy
I am trying to update a column in my dataframe based on the testing and results of the values within three columns (3 tests).
Some sample code:
df_test = pd.DataFrame([('?',2.0,1,0,0,0), (None,2.0,1,0,0,0),
(None,2.0,0,0,0,0),(None,2.0,0,1,0,0),
('?',2.0,0,0,0,0)], columns=['a','b','c','d','e','f'])
df_test.head()
When I try the following df_test['g'] = np.where(np.logical_and(df_test['a'] != 'None', df_test['c'] == 0, df_test['d'] == 0), True, False).astype(int)
I receive the error TypeError: return arrays must be of ArrayType
So I try the following:
df_test = pd.DataFrame([('?',2.0,1,0,0,0), (None,2.0,1,0,0,0),
(None,2.0,0,0,0,0),(None,2.0,0,1,0,0),
('?',2.0,0,0,0,0)], columns=['a','b','c','d','e','f'])
df_test['g'] = np.where(np.logical_and(df_test['a'] != None,
np.logical_and(df_test['c'] == 0,
df_test['d'] == 0)),
True, False).astype(int)
df_test.head()
And on row 2, where I would expect to see a 0 I see a 1 while row 4 appears correct. Test 1 (a), should be False while the second (c) and third (d) tests should be True, True. False == True == True is False.
a b c d e f g
0 ? 2.0 1 0 0 0 0
1 None 2.0 1 0 0 0 0
2 None 2.0 0 0 0 0 1
3 None 2.0 0 1 0 0 0
4 ? 2.0 0 0 0 0 1
I need a way to evaluate 3 tests and return true or false as an int.
python pandas numpy
python pandas numpy
asked Nov 12 '18 at 16:59
EricEric
1471214
1471214
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
For 3 or more conditions, use np.logical_and.reduce
and pass a list of masks;
mask = np.logical_and.reduce([
df_test['a'].notna(), df_test['c'].eq(0), df_test['d'].eq(0)])
df_test['g'] = mask.astype(int)
print(df_test)
a b c d e f g
0 ? 2.0 1 0 0 0 0
1 None 2.0 1 0 0 0 0
2 None 2.0 0 0 0 0 0
3 None 2.0 0 1 0 0 0
4 ? 2.0 0 0 0 0 1
The subsequent np.where
is superfluous here.
add a comment |
Try:
df_test['htest' ] = (df_test['a'].isna() & (df_test['c'] == 0) & (df_test['d'] == 0)).astype(int)
df_test['htest' ] = (df_test['a'].notna() & (df_test['c'] == 0) & (df_test['d'] == 0)).astype(int)
Output:
a b c d e f htest
0 ? 2.0 1 0 0 0 0
1 None 2.0 1 0 0 0 0
2 None 2.0 0 0 0 0 0
3 None 2.0 0 1 0 0 0
4 ? 2.0 0 0 0 0 1
Think the first condition should benotna
.
– coldspeed
Nov 12 '18 at 17:14
yes this works correctly in terms of multiple tests, changing isna() to notna() works as I would expect.
– Eric
Nov 12 '18 at 17:19
Ah.. Got thanks.
– Scott Boston
Nov 12 '18 at 17:22
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53266807%2fnp-logical-and-with-three-tests%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
For 3 or more conditions, use np.logical_and.reduce
and pass a list of masks;
mask = np.logical_and.reduce([
df_test['a'].notna(), df_test['c'].eq(0), df_test['d'].eq(0)])
df_test['g'] = mask.astype(int)
print(df_test)
a b c d e f g
0 ? 2.0 1 0 0 0 0
1 None 2.0 1 0 0 0 0
2 None 2.0 0 0 0 0 0
3 None 2.0 0 1 0 0 0
4 ? 2.0 0 0 0 0 1
The subsequent np.where
is superfluous here.
add a comment |
For 3 or more conditions, use np.logical_and.reduce
and pass a list of masks;
mask = np.logical_and.reduce([
df_test['a'].notna(), df_test['c'].eq(0), df_test['d'].eq(0)])
df_test['g'] = mask.astype(int)
print(df_test)
a b c d e f g
0 ? 2.0 1 0 0 0 0
1 None 2.0 1 0 0 0 0
2 None 2.0 0 0 0 0 0
3 None 2.0 0 1 0 0 0
4 ? 2.0 0 0 0 0 1
The subsequent np.where
is superfluous here.
add a comment |
For 3 or more conditions, use np.logical_and.reduce
and pass a list of masks;
mask = np.logical_and.reduce([
df_test['a'].notna(), df_test['c'].eq(0), df_test['d'].eq(0)])
df_test['g'] = mask.astype(int)
print(df_test)
a b c d e f g
0 ? 2.0 1 0 0 0 0
1 None 2.0 1 0 0 0 0
2 None 2.0 0 0 0 0 0
3 None 2.0 0 1 0 0 0
4 ? 2.0 0 0 0 0 1
The subsequent np.where
is superfluous here.
For 3 or more conditions, use np.logical_and.reduce
and pass a list of masks;
mask = np.logical_and.reduce([
df_test['a'].notna(), df_test['c'].eq(0), df_test['d'].eq(0)])
df_test['g'] = mask.astype(int)
print(df_test)
a b c d e f g
0 ? 2.0 1 0 0 0 0
1 None 2.0 1 0 0 0 0
2 None 2.0 0 0 0 0 0
3 None 2.0 0 1 0 0 0
4 ? 2.0 0 0 0 0 1
The subsequent np.where
is superfluous here.
answered Nov 12 '18 at 17:10
coldspeedcoldspeed
134k23145230
134k23145230
add a comment |
add a comment |
Try:
df_test['htest' ] = (df_test['a'].isna() & (df_test['c'] == 0) & (df_test['d'] == 0)).astype(int)
df_test['htest' ] = (df_test['a'].notna() & (df_test['c'] == 0) & (df_test['d'] == 0)).astype(int)
Output:
a b c d e f htest
0 ? 2.0 1 0 0 0 0
1 None 2.0 1 0 0 0 0
2 None 2.0 0 0 0 0 0
3 None 2.0 0 1 0 0 0
4 ? 2.0 0 0 0 0 1
Think the first condition should benotna
.
– coldspeed
Nov 12 '18 at 17:14
yes this works correctly in terms of multiple tests, changing isna() to notna() works as I would expect.
– Eric
Nov 12 '18 at 17:19
Ah.. Got thanks.
– Scott Boston
Nov 12 '18 at 17:22
add a comment |
Try:
df_test['htest' ] = (df_test['a'].isna() & (df_test['c'] == 0) & (df_test['d'] == 0)).astype(int)
df_test['htest' ] = (df_test['a'].notna() & (df_test['c'] == 0) & (df_test['d'] == 0)).astype(int)
Output:
a b c d e f htest
0 ? 2.0 1 0 0 0 0
1 None 2.0 1 0 0 0 0
2 None 2.0 0 0 0 0 0
3 None 2.0 0 1 0 0 0
4 ? 2.0 0 0 0 0 1
Think the first condition should benotna
.
– coldspeed
Nov 12 '18 at 17:14
yes this works correctly in terms of multiple tests, changing isna() to notna() works as I would expect.
– Eric
Nov 12 '18 at 17:19
Ah.. Got thanks.
– Scott Boston
Nov 12 '18 at 17:22
add a comment |
Try:
df_test['htest' ] = (df_test['a'].isna() & (df_test['c'] == 0) & (df_test['d'] == 0)).astype(int)
df_test['htest' ] = (df_test['a'].notna() & (df_test['c'] == 0) & (df_test['d'] == 0)).astype(int)
Output:
a b c d e f htest
0 ? 2.0 1 0 0 0 0
1 None 2.0 1 0 0 0 0
2 None 2.0 0 0 0 0 0
3 None 2.0 0 1 0 0 0
4 ? 2.0 0 0 0 0 1
Try:
df_test['htest' ] = (df_test['a'].isna() & (df_test['c'] == 0) & (df_test['d'] == 0)).astype(int)
df_test['htest' ] = (df_test['a'].notna() & (df_test['c'] == 0) & (df_test['d'] == 0)).astype(int)
Output:
a b c d e f htest
0 ? 2.0 1 0 0 0 0
1 None 2.0 1 0 0 0 0
2 None 2.0 0 0 0 0 0
3 None 2.0 0 1 0 0 0
4 ? 2.0 0 0 0 0 1
edited Nov 12 '18 at 17:21
answered Nov 12 '18 at 17:11
Scott BostonScott Boston
56.2k73157
56.2k73157
Think the first condition should benotna
.
– coldspeed
Nov 12 '18 at 17:14
yes this works correctly in terms of multiple tests, changing isna() to notna() works as I would expect.
– Eric
Nov 12 '18 at 17:19
Ah.. Got thanks.
– Scott Boston
Nov 12 '18 at 17:22
add a comment |
Think the first condition should benotna
.
– coldspeed
Nov 12 '18 at 17:14
yes this works correctly in terms of multiple tests, changing isna() to notna() works as I would expect.
– Eric
Nov 12 '18 at 17:19
Ah.. Got thanks.
– Scott Boston
Nov 12 '18 at 17:22
Think the first condition should be
notna
.– coldspeed
Nov 12 '18 at 17:14
Think the first condition should be
notna
.– coldspeed
Nov 12 '18 at 17:14
yes this works correctly in terms of multiple tests, changing isna() to notna() works as I would expect.
– Eric
Nov 12 '18 at 17:19
yes this works correctly in terms of multiple tests, changing isna() to notna() works as I would expect.
– Eric
Nov 12 '18 at 17:19
Ah.. Got thanks.
– Scott Boston
Nov 12 '18 at 17:22
Ah.. Got thanks.
– Scott Boston
Nov 12 '18 at 17:22
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53266807%2fnp-logical-and-with-three-tests%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown