Pandas, isin, column of lists












0















Trying to make a Boolean flag that reads TRUE if one value or another is within a list. The below code is returning a FALSE for row 1 and I am not sure why, could someone help me understand why a FALSE is getting returned for the first row?



lists={'someList!':[[1,2,12,6,'ABC'],[1000,4,'z','a','bob']]}
dfLists = pd.DataFrame(lists)
dfLists['contains?']=dfLists['someList!'].isin([0,1])









share|improve this question























  • Your code gives an error instead. Please provide a reproducible code.

    – TeeKea
    Nov 13 '18 at 16:54


















0















Trying to make a Boolean flag that reads TRUE if one value or another is within a list. The below code is returning a FALSE for row 1 and I am not sure why, could someone help me understand why a FALSE is getting returned for the first row?



lists={'someList!':[[1,2,12,6,'ABC'],[1000,4,'z','a','bob']]}
dfLists = pd.DataFrame(lists)
dfLists['contains?']=dfLists['someList!'].isin([0,1])









share|improve this question























  • Your code gives an error instead. Please provide a reproducible code.

    – TeeKea
    Nov 13 '18 at 16:54
















0












0








0








Trying to make a Boolean flag that reads TRUE if one value or another is within a list. The below code is returning a FALSE for row 1 and I am not sure why, could someone help me understand why a FALSE is getting returned for the first row?



lists={'someList!':[[1,2,12,6,'ABC'],[1000,4,'z','a','bob']]}
dfLists = pd.DataFrame(lists)
dfLists['contains?']=dfLists['someList!'].isin([0,1])









share|improve this question














Trying to make a Boolean flag that reads TRUE if one value or another is within a list. The below code is returning a FALSE for row 1 and I am not sure why, could someone help me understand why a FALSE is getting returned for the first row?



lists={'someList!':[[1,2,12,6,'ABC'],[1000,4,'z','a','bob']]}
dfLists = pd.DataFrame(lists)
dfLists['contains?']=dfLists['someList!'].isin([0,1])






pandas






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 16:43









reality stiltsreality stilts

435




435













  • Your code gives an error instead. Please provide a reproducible code.

    – TeeKea
    Nov 13 '18 at 16:54





















  • Your code gives an error instead. Please provide a reproducible code.

    – TeeKea
    Nov 13 '18 at 16:54



















Your code gives an error instead. Please provide a reproducible code.

– TeeKea
Nov 13 '18 at 16:54







Your code gives an error instead. Please provide a reproducible code.

– TeeKea
Nov 13 '18 at 16:54














3 Answers
3






active

oldest

votes


















0















Could someone help me understand why a FALSE is getting returned for the first row?




This isn't working because .isin(values) returns whether each element in the Series is contained in values.



You can use {0, 1} as a set and apply the truthiness of its intersection to each list:



>>> s = {0, 1}
>>> dfLists['someList!'].apply(lambda x: bool(s.intersection(x)))
0 True
1 False


This effectively does:



>>> s.intersection([1, 2, 12, 6, 'ABC'])
{1}
>>> s.intersection([1000, 4, 'z', 'a', 'bob'])
set()


The bool of the first result is True, because it is non-empty.






share|improve this answer































    0














    Using Dataframe constructor flatten you list column, then using isin



    pd.DataFrame(dfLists['someList!'].tolist()).isin([1,2]).any(1)
    Out[39]:
    0 True
    1 False
    dtype: bool





    share|improve this answer
























    • thx for this answer, found it helpful

      – reality stilts
      Nov 13 '18 at 17:58



















    0














    You are passing the dataframe a list of lists. It's comparing integers to lists, so it's not finding a match.



    Define your columns distinctly, like this, and isin should work.



    lists={'someList!':[1,2,12,6,'ABC'], 'someList2':[1000,4,'z','a','bob']}





    share|improve this answer























      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%2f53285716%2fpandas-isin-column-of-lists%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0















      Could someone help me understand why a FALSE is getting returned for the first row?




      This isn't working because .isin(values) returns whether each element in the Series is contained in values.



      You can use {0, 1} as a set and apply the truthiness of its intersection to each list:



      >>> s = {0, 1}
      >>> dfLists['someList!'].apply(lambda x: bool(s.intersection(x)))
      0 True
      1 False


      This effectively does:



      >>> s.intersection([1, 2, 12, 6, 'ABC'])
      {1}
      >>> s.intersection([1000, 4, 'z', 'a', 'bob'])
      set()


      The bool of the first result is True, because it is non-empty.






      share|improve this answer




























        0















        Could someone help me understand why a FALSE is getting returned for the first row?




        This isn't working because .isin(values) returns whether each element in the Series is contained in values.



        You can use {0, 1} as a set and apply the truthiness of its intersection to each list:



        >>> s = {0, 1}
        >>> dfLists['someList!'].apply(lambda x: bool(s.intersection(x)))
        0 True
        1 False


        This effectively does:



        >>> s.intersection([1, 2, 12, 6, 'ABC'])
        {1}
        >>> s.intersection([1000, 4, 'z', 'a', 'bob'])
        set()


        The bool of the first result is True, because it is non-empty.






        share|improve this answer


























          0












          0








          0








          Could someone help me understand why a FALSE is getting returned for the first row?




          This isn't working because .isin(values) returns whether each element in the Series is contained in values.



          You can use {0, 1} as a set and apply the truthiness of its intersection to each list:



          >>> s = {0, 1}
          >>> dfLists['someList!'].apply(lambda x: bool(s.intersection(x)))
          0 True
          1 False


          This effectively does:



          >>> s.intersection([1, 2, 12, 6, 'ABC'])
          {1}
          >>> s.intersection([1000, 4, 'z', 'a', 'bob'])
          set()


          The bool of the first result is True, because it is non-empty.






          share|improve this answer














          Could someone help me understand why a FALSE is getting returned for the first row?




          This isn't working because .isin(values) returns whether each element in the Series is contained in values.



          You can use {0, 1} as a set and apply the truthiness of its intersection to each list:



          >>> s = {0, 1}
          >>> dfLists['someList!'].apply(lambda x: bool(s.intersection(x)))
          0 True
          1 False


          This effectively does:



          >>> s.intersection([1, 2, 12, 6, 'ABC'])
          {1}
          >>> s.intersection([1000, 4, 'z', 'a', 'bob'])
          set()


          The bool of the first result is True, because it is non-empty.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 13 '18 at 16:54









          Brad SolomonBrad Solomon

          13.7k73484




          13.7k73484

























              0














              Using Dataframe constructor flatten you list column, then using isin



              pd.DataFrame(dfLists['someList!'].tolist()).isin([1,2]).any(1)
              Out[39]:
              0 True
              1 False
              dtype: bool





              share|improve this answer
























              • thx for this answer, found it helpful

                – reality stilts
                Nov 13 '18 at 17:58
















              0














              Using Dataframe constructor flatten you list column, then using isin



              pd.DataFrame(dfLists['someList!'].tolist()).isin([1,2]).any(1)
              Out[39]:
              0 True
              1 False
              dtype: bool





              share|improve this answer
























              • thx for this answer, found it helpful

                – reality stilts
                Nov 13 '18 at 17:58














              0












              0








              0







              Using Dataframe constructor flatten you list column, then using isin



              pd.DataFrame(dfLists['someList!'].tolist()).isin([1,2]).any(1)
              Out[39]:
              0 True
              1 False
              dtype: bool





              share|improve this answer













              Using Dataframe constructor flatten you list column, then using isin



              pd.DataFrame(dfLists['someList!'].tolist()).isin([1,2]).any(1)
              Out[39]:
              0 True
              1 False
              dtype: bool






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 13 '18 at 16:55









              Wen-BenWen-Ben

              1




              1













              • thx for this answer, found it helpful

                – reality stilts
                Nov 13 '18 at 17:58



















              • thx for this answer, found it helpful

                – reality stilts
                Nov 13 '18 at 17:58

















              thx for this answer, found it helpful

              – reality stilts
              Nov 13 '18 at 17:58





              thx for this answer, found it helpful

              – reality stilts
              Nov 13 '18 at 17:58











              0














              You are passing the dataframe a list of lists. It's comparing integers to lists, so it's not finding a match.



              Define your columns distinctly, like this, and isin should work.



              lists={'someList!':[1,2,12,6,'ABC'], 'someList2':[1000,4,'z','a','bob']}





              share|improve this answer




























                0














                You are passing the dataframe a list of lists. It's comparing integers to lists, so it's not finding a match.



                Define your columns distinctly, like this, and isin should work.



                lists={'someList!':[1,2,12,6,'ABC'], 'someList2':[1000,4,'z','a','bob']}





                share|improve this answer


























                  0












                  0








                  0







                  You are passing the dataframe a list of lists. It's comparing integers to lists, so it's not finding a match.



                  Define your columns distinctly, like this, and isin should work.



                  lists={'someList!':[1,2,12,6,'ABC'], 'someList2':[1000,4,'z','a','bob']}





                  share|improve this answer













                  You are passing the dataframe a list of lists. It's comparing integers to lists, so it's not finding a match.



                  Define your columns distinctly, like this, and isin should work.



                  lists={'someList!':[1,2,12,6,'ABC'], 'someList2':[1000,4,'z','a','bob']}






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 13 '18 at 16:56









                  CobraCobra

                  225




                  225






























                      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%2f53285716%2fpandas-isin-column-of-lists%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

                      Coverage of Google Street View

                      Full-time equivalent

                      Surfing