Pandas: compare list objects in Series

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
6
down vote

favorite












In my dataframe a column is made up of lists, for example:



df = pd.DataFrame('A':[[1,2],[2,4],[3,1]])


I need to find out the location of list [1,2] in this dataframe. I tried:



df.loc[df['A'] == [1,2]]


and



df.loc[df['A'] == [[1,2]]]


but failed totally. The comparison seems very simple but that just doesn't work. Am I missing something here?










share|improve this question









New contributor




Shiang Hoo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.























    up vote
    6
    down vote

    favorite












    In my dataframe a column is made up of lists, for example:



    df = pd.DataFrame('A':[[1,2],[2,4],[3,1]])


    I need to find out the location of list [1,2] in this dataframe. I tried:



    df.loc[df['A'] == [1,2]]


    and



    df.loc[df['A'] == [[1,2]]]


    but failed totally. The comparison seems very simple but that just doesn't work. Am I missing something here?










    share|improve this question









    New contributor




    Shiang Hoo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.





















      up vote
      6
      down vote

      favorite









      up vote
      6
      down vote

      favorite











      In my dataframe a column is made up of lists, for example:



      df = pd.DataFrame('A':[[1,2],[2,4],[3,1]])


      I need to find out the location of list [1,2] in this dataframe. I tried:



      df.loc[df['A'] == [1,2]]


      and



      df.loc[df['A'] == [[1,2]]]


      but failed totally. The comparison seems very simple but that just doesn't work. Am I missing something here?










      share|improve this question









      New contributor




      Shiang Hoo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      In my dataframe a column is made up of lists, for example:



      df = pd.DataFrame('A':[[1,2],[2,4],[3,1]])


      I need to find out the location of list [1,2] in this dataframe. I tried:



      df.loc[df['A'] == [1,2]]


      and



      df.loc[df['A'] == [[1,2]]]


      but failed totally. The comparison seems very simple but that just doesn't work. Am I missing something here?







      python pandas






      share|improve this question









      New contributor




      Shiang Hoo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      Shiang Hoo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited 1 hour ago









      Seanny123

      2,18833261




      2,18833261






      New contributor




      Shiang Hoo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 2 hours ago









      Shiang Hoo

      311




      311




      New contributor




      Shiang Hoo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Shiang Hoo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Shiang Hoo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          4 Answers
          4






          active

          oldest

          votes

















          up vote
          6
          down vote













          Do not using list in cell , it create a lot of problem for pandas .If you do need a object columns , using tuple



          df.A.map(tuple).isin([(1,2)])
          Out[293]:
          0 True
          1 False
          2 False
          Name: A, dtype: bool
          #df[df.A.map(tuple).isin([(1,2)])]





          share|improve this answer



























            up vote
            5
            down vote













            With Numpy arrays



            df.assign(B=(np.array(df.A.tolist()) == [1, 2]).all(1))

            A B
            0 [1, 2] True
            1 [2, 4] False
            2 [3, 1] False





            share|improve this answer
















            • 2




              This should be the accepted solution! [Or, if possible, just expanding the series of lists to 2 series.]
              – jpp
              1 hour ago










            • Won't this run into issues if the lists are differently sized, though perhaps that's outside of the scope of this example.
              – ALollz
              21 mins ago











            • @ALollz yes and yes
              – piRSquared
              8 mins ago

















            up vote
            3
            down vote













            You can use apply and compare as:



            df['A'].apply(lambda x: x==[1,2])

            0 True
            1 False
            2 False
            Name: A, dtype: bool



            print(df[df['A'].apply(lambda x: x==[1,2])])

            A
            0 [1, 2]





            share|improve this answer



























              up vote
              2
              down vote













              Little messy solution using numpy



              df.A.apply(lambda x: (np.array(x) == np.array([1,2])).all())

              0 True
              1 False
              2 False





              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',
                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
                );



                );






                Shiang Hoo is a new contributor. Be nice, and check out our Code of Conduct.









                 

                draft saved


                draft discarded


















                StackExchange.ready(
                function ()
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53102731%2fpandas-compare-list-objects-in-series%23new-answer', 'question_page');

                );

                Post as a guest






























                4 Answers
                4






                active

                oldest

                votes








                4 Answers
                4






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes








                up vote
                6
                down vote













                Do not using list in cell , it create a lot of problem for pandas .If you do need a object columns , using tuple



                df.A.map(tuple).isin([(1,2)])
                Out[293]:
                0 True
                1 False
                2 False
                Name: A, dtype: bool
                #df[df.A.map(tuple).isin([(1,2)])]





                share|improve this answer
























                  up vote
                  6
                  down vote













                  Do not using list in cell , it create a lot of problem for pandas .If you do need a object columns , using tuple



                  df.A.map(tuple).isin([(1,2)])
                  Out[293]:
                  0 True
                  1 False
                  2 False
                  Name: A, dtype: bool
                  #df[df.A.map(tuple).isin([(1,2)])]





                  share|improve this answer






















                    up vote
                    6
                    down vote










                    up vote
                    6
                    down vote









                    Do not using list in cell , it create a lot of problem for pandas .If you do need a object columns , using tuple



                    df.A.map(tuple).isin([(1,2)])
                    Out[293]:
                    0 True
                    1 False
                    2 False
                    Name: A, dtype: bool
                    #df[df.A.map(tuple).isin([(1,2)])]





                    share|improve this answer












                    Do not using list in cell , it create a lot of problem for pandas .If you do need a object columns , using tuple



                    df.A.map(tuple).isin([(1,2)])
                    Out[293]:
                    0 True
                    1 False
                    2 False
                    Name: A, dtype: bool
                    #df[df.A.map(tuple).isin([(1,2)])]






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered 2 hours ago









                    W-B

                    89.1k72653




                    89.1k72653






















                        up vote
                        5
                        down vote













                        With Numpy arrays



                        df.assign(B=(np.array(df.A.tolist()) == [1, 2]).all(1))

                        A B
                        0 [1, 2] True
                        1 [2, 4] False
                        2 [3, 1] False





                        share|improve this answer
















                        • 2




                          This should be the accepted solution! [Or, if possible, just expanding the series of lists to 2 series.]
                          – jpp
                          1 hour ago










                        • Won't this run into issues if the lists are differently sized, though perhaps that's outside of the scope of this example.
                          – ALollz
                          21 mins ago











                        • @ALollz yes and yes
                          – piRSquared
                          8 mins ago














                        up vote
                        5
                        down vote













                        With Numpy arrays



                        df.assign(B=(np.array(df.A.tolist()) == [1, 2]).all(1))

                        A B
                        0 [1, 2] True
                        1 [2, 4] False
                        2 [3, 1] False





                        share|improve this answer
















                        • 2




                          This should be the accepted solution! [Or, if possible, just expanding the series of lists to 2 series.]
                          – jpp
                          1 hour ago










                        • Won't this run into issues if the lists are differently sized, though perhaps that's outside of the scope of this example.
                          – ALollz
                          21 mins ago











                        • @ALollz yes and yes
                          – piRSquared
                          8 mins ago












                        up vote
                        5
                        down vote










                        up vote
                        5
                        down vote









                        With Numpy arrays



                        df.assign(B=(np.array(df.A.tolist()) == [1, 2]).all(1))

                        A B
                        0 [1, 2] True
                        1 [2, 4] False
                        2 [3, 1] False





                        share|improve this answer












                        With Numpy arrays



                        df.assign(B=(np.array(df.A.tolist()) == [1, 2]).all(1))

                        A B
                        0 [1, 2] True
                        1 [2, 4] False
                        2 [3, 1] False






                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered 1 hour ago









                        piRSquared

                        146k21128260




                        146k21128260







                        • 2




                          This should be the accepted solution! [Or, if possible, just expanding the series of lists to 2 series.]
                          – jpp
                          1 hour ago










                        • Won't this run into issues if the lists are differently sized, though perhaps that's outside of the scope of this example.
                          – ALollz
                          21 mins ago











                        • @ALollz yes and yes
                          – piRSquared
                          8 mins ago












                        • 2




                          This should be the accepted solution! [Or, if possible, just expanding the series of lists to 2 series.]
                          – jpp
                          1 hour ago










                        • Won't this run into issues if the lists are differently sized, though perhaps that's outside of the scope of this example.
                          – ALollz
                          21 mins ago











                        • @ALollz yes and yes
                          – piRSquared
                          8 mins ago







                        2




                        2




                        This should be the accepted solution! [Or, if possible, just expanding the series of lists to 2 series.]
                        – jpp
                        1 hour ago




                        This should be the accepted solution! [Or, if possible, just expanding the series of lists to 2 series.]
                        – jpp
                        1 hour ago












                        Won't this run into issues if the lists are differently sized, though perhaps that's outside of the scope of this example.
                        – ALollz
                        21 mins ago





                        Won't this run into issues if the lists are differently sized, though perhaps that's outside of the scope of this example.
                        – ALollz
                        21 mins ago













                        @ALollz yes and yes
                        – piRSquared
                        8 mins ago




                        @ALollz yes and yes
                        – piRSquared
                        8 mins ago










                        up vote
                        3
                        down vote













                        You can use apply and compare as:



                        df['A'].apply(lambda x: x==[1,2])

                        0 True
                        1 False
                        2 False
                        Name: A, dtype: bool



                        print(df[df['A'].apply(lambda x: x==[1,2])])

                        A
                        0 [1, 2]





                        share|improve this answer
























                          up vote
                          3
                          down vote













                          You can use apply and compare as:



                          df['A'].apply(lambda x: x==[1,2])

                          0 True
                          1 False
                          2 False
                          Name: A, dtype: bool



                          print(df[df['A'].apply(lambda x: x==[1,2])])

                          A
                          0 [1, 2]





                          share|improve this answer






















                            up vote
                            3
                            down vote










                            up vote
                            3
                            down vote









                            You can use apply and compare as:



                            df['A'].apply(lambda x: x==[1,2])

                            0 True
                            1 False
                            2 False
                            Name: A, dtype: bool



                            print(df[df['A'].apply(lambda x: x==[1,2])])

                            A
                            0 [1, 2]





                            share|improve this answer












                            You can use apply and compare as:



                            df['A'].apply(lambda x: x==[1,2])

                            0 True
                            1 False
                            2 False
                            Name: A, dtype: bool



                            print(df[df['A'].apply(lambda x: x==[1,2])])

                            A
                            0 [1, 2]






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered 2 hours ago









                            Sandeep Kadapa

                            3,524424




                            3,524424




















                                up vote
                                2
                                down vote













                                Little messy solution using numpy



                                df.A.apply(lambda x: (np.array(x) == np.array([1,2])).all())

                                0 True
                                1 False
                                2 False





                                share|improve this answer
























                                  up vote
                                  2
                                  down vote













                                  Little messy solution using numpy



                                  df.A.apply(lambda x: (np.array(x) == np.array([1,2])).all())

                                  0 True
                                  1 False
                                  2 False





                                  share|improve this answer






















                                    up vote
                                    2
                                    down vote










                                    up vote
                                    2
                                    down vote









                                    Little messy solution using numpy



                                    df.A.apply(lambda x: (np.array(x) == np.array([1,2])).all())

                                    0 True
                                    1 False
                                    2 False





                                    share|improve this answer












                                    Little messy solution using numpy



                                    df.A.apply(lambda x: (np.array(x) == np.array([1,2])).all())

                                    0 True
                                    1 False
                                    2 False






                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered 1 hour ago









                                    Vaishali

                                    16k3927




                                    16k3927




















                                        Shiang Hoo is a new contributor. Be nice, and check out our Code of Conduct.









                                         

                                        draft saved


                                        draft discarded


















                                        Shiang Hoo is a new contributor. Be nice, and check out our Code of Conduct.












                                        Shiang Hoo is a new contributor. Be nice, and check out our Code of Conduct.











                                        Shiang Hoo is a new contributor. Be nice, and check out our Code of Conduct.













                                         


                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function ()
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53102731%2fpandas-compare-list-objects-in-series%23new-answer', 'question_page');

                                        );

                                        Post as a guest













































































                                        Comments

                                        Popular posts from this blog

                                        What does second last employer means? [closed]

                                        Installing NextGIS Connect into QGIS 3?

                                        One-line joke