Python If x==True, and if y==False then start array true












0















I have two dataframes.



PPASbool=
0 True
1 True
2 False
3 True
4 False

CPASbool=
0 True
1 False
2 False
3 False
4 True


I tried this below, but I received an error



 File "Typology.py", line 66, in <module>
if PPASbool==True :
File "C:UsersJimAnaconda3libsite-packagespandascoregeneric.py",
line 1573, in __nonzero__
.format(self.__class__.__name__))
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

if PPASbool==True :
if CPASbool == False :
PeerNoCo=True
else:
PeerNoCo=False
else:
PeerNoCo=False


I want to put True in a new Array called PeerNoCo if PPASbool is true and CPAS is False.



Expected output
PeerNoCo=
0 False
1 True
2 False
3 True
4 False









share|improve this question


















  • 5





    This is simply a logical and with the bitwise NOT of one of the series. a & ~b if a and b are the two series.

    – user3483203
    Nov 20 '18 at 19:06








  • 2





    The "ambiguous" value error happens when you try to evaluate a series to a signle boolean. For example if you have a series, A that equals to T, F, T. A == F is ambiguous, because some of A is False and other parts are True. On way to add more clarity depending on logic is to use all or any. if (A == True).all() or (A == True).any() to return a single True or False.

    – Scott Boston
    Nov 20 '18 at 19:10











  • Just to add, typically when trying to do logical if else, or if, elif else statements row-wise you would look to numpy.where or numpy.select, respectively. Though @user3483203 s solution is the most appropriate in this case.

    – ALollz
    Nov 20 '18 at 19:15
















0















I have two dataframes.



PPASbool=
0 True
1 True
2 False
3 True
4 False

CPASbool=
0 True
1 False
2 False
3 False
4 True


I tried this below, but I received an error



 File "Typology.py", line 66, in <module>
if PPASbool==True :
File "C:UsersJimAnaconda3libsite-packagespandascoregeneric.py",
line 1573, in __nonzero__
.format(self.__class__.__name__))
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

if PPASbool==True :
if CPASbool == False :
PeerNoCo=True
else:
PeerNoCo=False
else:
PeerNoCo=False


I want to put True in a new Array called PeerNoCo if PPASbool is true and CPAS is False.



Expected output
PeerNoCo=
0 False
1 True
2 False
3 True
4 False









share|improve this question


















  • 5





    This is simply a logical and with the bitwise NOT of one of the series. a & ~b if a and b are the two series.

    – user3483203
    Nov 20 '18 at 19:06








  • 2





    The "ambiguous" value error happens when you try to evaluate a series to a signle boolean. For example if you have a series, A that equals to T, F, T. A == F is ambiguous, because some of A is False and other parts are True. On way to add more clarity depending on logic is to use all or any. if (A == True).all() or (A == True).any() to return a single True or False.

    – Scott Boston
    Nov 20 '18 at 19:10











  • Just to add, typically when trying to do logical if else, or if, elif else statements row-wise you would look to numpy.where or numpy.select, respectively. Though @user3483203 s solution is the most appropriate in this case.

    – ALollz
    Nov 20 '18 at 19:15














0












0








0








I have two dataframes.



PPASbool=
0 True
1 True
2 False
3 True
4 False

CPASbool=
0 True
1 False
2 False
3 False
4 True


I tried this below, but I received an error



 File "Typology.py", line 66, in <module>
if PPASbool==True :
File "C:UsersJimAnaconda3libsite-packagespandascoregeneric.py",
line 1573, in __nonzero__
.format(self.__class__.__name__))
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

if PPASbool==True :
if CPASbool == False :
PeerNoCo=True
else:
PeerNoCo=False
else:
PeerNoCo=False


I want to put True in a new Array called PeerNoCo if PPASbool is true and CPAS is False.



Expected output
PeerNoCo=
0 False
1 True
2 False
3 True
4 False









share|improve this question














I have two dataframes.



PPASbool=
0 True
1 True
2 False
3 True
4 False

CPASbool=
0 True
1 False
2 False
3 False
4 True


I tried this below, but I received an error



 File "Typology.py", line 66, in <module>
if PPASbool==True :
File "C:UsersJimAnaconda3libsite-packagespandascoregeneric.py",
line 1573, in __nonzero__
.format(self.__class__.__name__))
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

if PPASbool==True :
if CPASbool == False :
PeerNoCo=True
else:
PeerNoCo=False
else:
PeerNoCo=False


I want to put True in a new Array called PeerNoCo if PPASbool is true and CPAS is False.



Expected output
PeerNoCo=
0 False
1 True
2 False
3 True
4 False






python python-3.x pandas numpy






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 20 '18 at 19:04









Jim HubbardJim Hubbard

194




194








  • 5





    This is simply a logical and with the bitwise NOT of one of the series. a & ~b if a and b are the two series.

    – user3483203
    Nov 20 '18 at 19:06








  • 2





    The "ambiguous" value error happens when you try to evaluate a series to a signle boolean. For example if you have a series, A that equals to T, F, T. A == F is ambiguous, because some of A is False and other parts are True. On way to add more clarity depending on logic is to use all or any. if (A == True).all() or (A == True).any() to return a single True or False.

    – Scott Boston
    Nov 20 '18 at 19:10











  • Just to add, typically when trying to do logical if else, or if, elif else statements row-wise you would look to numpy.where or numpy.select, respectively. Though @user3483203 s solution is the most appropriate in this case.

    – ALollz
    Nov 20 '18 at 19:15














  • 5





    This is simply a logical and with the bitwise NOT of one of the series. a & ~b if a and b are the two series.

    – user3483203
    Nov 20 '18 at 19:06








  • 2





    The "ambiguous" value error happens when you try to evaluate a series to a signle boolean. For example if you have a series, A that equals to T, F, T. A == F is ambiguous, because some of A is False and other parts are True. On way to add more clarity depending on logic is to use all or any. if (A == True).all() or (A == True).any() to return a single True or False.

    – Scott Boston
    Nov 20 '18 at 19:10











  • Just to add, typically when trying to do logical if else, or if, elif else statements row-wise you would look to numpy.where or numpy.select, respectively. Though @user3483203 s solution is the most appropriate in this case.

    – ALollz
    Nov 20 '18 at 19:15








5




5





This is simply a logical and with the bitwise NOT of one of the series. a & ~b if a and b are the two series.

– user3483203
Nov 20 '18 at 19:06







This is simply a logical and with the bitwise NOT of one of the series. a & ~b if a and b are the two series.

– user3483203
Nov 20 '18 at 19:06






2




2





The "ambiguous" value error happens when you try to evaluate a series to a signle boolean. For example if you have a series, A that equals to T, F, T. A == F is ambiguous, because some of A is False and other parts are True. On way to add more clarity depending on logic is to use all or any. if (A == True).all() or (A == True).any() to return a single True or False.

– Scott Boston
Nov 20 '18 at 19:10





The "ambiguous" value error happens when you try to evaluate a series to a signle boolean. For example if you have a series, A that equals to T, F, T. A == F is ambiguous, because some of A is False and other parts are True. On way to add more clarity depending on logic is to use all or any. if (A == True).all() or (A == True).any() to return a single True or False.

– Scott Boston
Nov 20 '18 at 19:10













Just to add, typically when trying to do logical if else, or if, elif else statements row-wise you would look to numpy.where or numpy.select, respectively. Though @user3483203 s solution is the most appropriate in this case.

– ALollz
Nov 20 '18 at 19:15





Just to add, typically when trying to do logical if else, or if, elif else statements row-wise you would look to numpy.where or numpy.select, respectively. Though @user3483203 s solution is the most appropriate in this case.

– ALollz
Nov 20 '18 at 19:15












1 Answer
1






active

oldest

votes


















1














2 things:




  1. you don't have to say == True. If is automatically testing a condition. If you want to condition on variable temp being True, you can just do if temp:


  2. You're trying to do a conditional on a series. You can iterate over your values. If you want to maintain indices, you can use enumerate().



EDIT:



As one of your comments have mentioned, the easiest way to achieve what you want is just to store PPASbool & ~CPAS.






share|improve this answer





















  • 1





    I generally agree that ==True is redundant. However if the dtype is not bool (perhaps it was reindexed and now contains NaN and is object) then it's necessary.

    – ALollz
    Nov 20 '18 at 19:17








  • 1





    Definitely valid- thanks for pointing that out.

    – John Rouhana
    Nov 20 '18 at 19:19











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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53399846%2fpython-if-x-true-and-if-y-false-then-start-array-true%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














2 things:




  1. you don't have to say == True. If is automatically testing a condition. If you want to condition on variable temp being True, you can just do if temp:


  2. You're trying to do a conditional on a series. You can iterate over your values. If you want to maintain indices, you can use enumerate().



EDIT:



As one of your comments have mentioned, the easiest way to achieve what you want is just to store PPASbool & ~CPAS.






share|improve this answer





















  • 1





    I generally agree that ==True is redundant. However if the dtype is not bool (perhaps it was reindexed and now contains NaN and is object) then it's necessary.

    – ALollz
    Nov 20 '18 at 19:17








  • 1





    Definitely valid- thanks for pointing that out.

    – John Rouhana
    Nov 20 '18 at 19:19
















1














2 things:




  1. you don't have to say == True. If is automatically testing a condition. If you want to condition on variable temp being True, you can just do if temp:


  2. You're trying to do a conditional on a series. You can iterate over your values. If you want to maintain indices, you can use enumerate().



EDIT:



As one of your comments have mentioned, the easiest way to achieve what you want is just to store PPASbool & ~CPAS.






share|improve this answer





















  • 1





    I generally agree that ==True is redundant. However if the dtype is not bool (perhaps it was reindexed and now contains NaN and is object) then it's necessary.

    – ALollz
    Nov 20 '18 at 19:17








  • 1





    Definitely valid- thanks for pointing that out.

    – John Rouhana
    Nov 20 '18 at 19:19














1












1








1







2 things:




  1. you don't have to say == True. If is automatically testing a condition. If you want to condition on variable temp being True, you can just do if temp:


  2. You're trying to do a conditional on a series. You can iterate over your values. If you want to maintain indices, you can use enumerate().



EDIT:



As one of your comments have mentioned, the easiest way to achieve what you want is just to store PPASbool & ~CPAS.






share|improve this answer















2 things:




  1. you don't have to say == True. If is automatically testing a condition. If you want to condition on variable temp being True, you can just do if temp:


  2. You're trying to do a conditional on a series. You can iterate over your values. If you want to maintain indices, you can use enumerate().



EDIT:



As one of your comments have mentioned, the easiest way to achieve what you want is just to store PPASbool & ~CPAS.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 20 '18 at 19:18

























answered Nov 20 '18 at 19:09









John RouhanaJohn Rouhana

1779




1779








  • 1





    I generally agree that ==True is redundant. However if the dtype is not bool (perhaps it was reindexed and now contains NaN and is object) then it's necessary.

    – ALollz
    Nov 20 '18 at 19:17








  • 1





    Definitely valid- thanks for pointing that out.

    – John Rouhana
    Nov 20 '18 at 19:19














  • 1





    I generally agree that ==True is redundant. However if the dtype is not bool (perhaps it was reindexed and now contains NaN and is object) then it's necessary.

    – ALollz
    Nov 20 '18 at 19:17








  • 1





    Definitely valid- thanks for pointing that out.

    – John Rouhana
    Nov 20 '18 at 19:19








1




1





I generally agree that ==True is redundant. However if the dtype is not bool (perhaps it was reindexed and now contains NaN and is object) then it's necessary.

– ALollz
Nov 20 '18 at 19:17







I generally agree that ==True is redundant. However if the dtype is not bool (perhaps it was reindexed and now contains NaN and is object) then it's necessary.

– ALollz
Nov 20 '18 at 19:17






1




1





Definitely valid- thanks for pointing that out.

– John Rouhana
Nov 20 '18 at 19:19





Definitely valid- thanks for pointing that out.

– John Rouhana
Nov 20 '18 at 19:19


















draft saved

draft discarded




















































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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53399846%2fpython-if-x-true-and-if-y-false-then-start-array-true%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

How to fix TextFormField cause rebuild widget in Flutter