why sort_values() is diifferent form sort_values().values










0















I want to sort a dataframe by all columns,and I find a way to solve that using



df = df.apply( lambda x: x.sort_values()) 


and I used it to my data



text1 = text
text = text.apply( lambda x : x.sort_values())
text1 = text1.apply( lambda x : x.sort_values().values)
text.head()
text1.head()


why not text = text.apply( lambda x : x.sort_values()) get a wrong answer,and what is the .vaules)function?



text.head()
Wave 2881.394531 2880.574219 2879.75293 2878.931641 2878.111328
N-1 0.220934 0.203666 0.205743 0.196011 0.176293
N-10 0.432692 0.387074 0.395692 0.355331 0.358963
N-11 0.483360 0.463233 0.456304 0.428930 0.421482
N-12 0.365057 0.364417 0.385134 0.352451 0.350513
N-13 0.492172 0.466263 0.480657 0.439115 0.404883


text1.head()
Wave 2881.394531 2880.574219 2879.75293 2878.931641 2878.111328
P+1 -21.297623 -25.141329 -21.097095 -31.380476 -38.847958
P+2 -12.681051 -14.661134 -13.688742 -16.829298 -20.320133
P+3 -8.164744 -13.097990 -11.784309 -15.419610 -17.822252
P+4 -0.023353 -0.926852 -8.036203 -14.583183 -17.071484
P+5 0.022854 -0.037756 -0.002519 -1.891178 -7.795961









share|improve this question




























    0















    I want to sort a dataframe by all columns,and I find a way to solve that using



    df = df.apply( lambda x: x.sort_values()) 


    and I used it to my data



    text1 = text
    text = text.apply( lambda x : x.sort_values())
    text1 = text1.apply( lambda x : x.sort_values().values)
    text.head()
    text1.head()


    why not text = text.apply( lambda x : x.sort_values()) get a wrong answer,and what is the .vaules)function?



    text.head()
    Wave 2881.394531 2880.574219 2879.75293 2878.931641 2878.111328
    N-1 0.220934 0.203666 0.205743 0.196011 0.176293
    N-10 0.432692 0.387074 0.395692 0.355331 0.358963
    N-11 0.483360 0.463233 0.456304 0.428930 0.421482
    N-12 0.365057 0.364417 0.385134 0.352451 0.350513
    N-13 0.492172 0.466263 0.480657 0.439115 0.404883


    text1.head()
    Wave 2881.394531 2880.574219 2879.75293 2878.931641 2878.111328
    P+1 -21.297623 -25.141329 -21.097095 -31.380476 -38.847958
    P+2 -12.681051 -14.661134 -13.688742 -16.829298 -20.320133
    P+3 -8.164744 -13.097990 -11.784309 -15.419610 -17.822252
    P+4 -0.023353 -0.926852 -8.036203 -14.583183 -17.071484
    P+5 0.022854 -0.037756 -0.002519 -1.891178 -7.795961









    share|improve this question


























      0












      0








      0


      1






      I want to sort a dataframe by all columns,and I find a way to solve that using



      df = df.apply( lambda x: x.sort_values()) 


      and I used it to my data



      text1 = text
      text = text.apply( lambda x : x.sort_values())
      text1 = text1.apply( lambda x : x.sort_values().values)
      text.head()
      text1.head()


      why not text = text.apply( lambda x : x.sort_values()) get a wrong answer,and what is the .vaules)function?



      text.head()
      Wave 2881.394531 2880.574219 2879.75293 2878.931641 2878.111328
      N-1 0.220934 0.203666 0.205743 0.196011 0.176293
      N-10 0.432692 0.387074 0.395692 0.355331 0.358963
      N-11 0.483360 0.463233 0.456304 0.428930 0.421482
      N-12 0.365057 0.364417 0.385134 0.352451 0.350513
      N-13 0.492172 0.466263 0.480657 0.439115 0.404883


      text1.head()
      Wave 2881.394531 2880.574219 2879.75293 2878.931641 2878.111328
      P+1 -21.297623 -25.141329 -21.097095 -31.380476 -38.847958
      P+2 -12.681051 -14.661134 -13.688742 -16.829298 -20.320133
      P+3 -8.164744 -13.097990 -11.784309 -15.419610 -17.822252
      P+4 -0.023353 -0.926852 -8.036203 -14.583183 -17.071484
      P+5 0.022854 -0.037756 -0.002519 -1.891178 -7.795961









      share|improve this question
















      I want to sort a dataframe by all columns,and I find a way to solve that using



      df = df.apply( lambda x: x.sort_values()) 


      and I used it to my data



      text1 = text
      text = text.apply( lambda x : x.sort_values())
      text1 = text1.apply( lambda x : x.sort_values().values)
      text.head()
      text1.head()


      why not text = text.apply( lambda x : x.sort_values()) get a wrong answer,and what is the .vaules)function?



      text.head()
      Wave 2881.394531 2880.574219 2879.75293 2878.931641 2878.111328
      N-1 0.220934 0.203666 0.205743 0.196011 0.176293
      N-10 0.432692 0.387074 0.395692 0.355331 0.358963
      N-11 0.483360 0.463233 0.456304 0.428930 0.421482
      N-12 0.365057 0.364417 0.385134 0.352451 0.350513
      N-13 0.492172 0.466263 0.480657 0.439115 0.404883


      text1.head()
      Wave 2881.394531 2880.574219 2879.75293 2878.931641 2878.111328
      P+1 -21.297623 -25.141329 -21.097095 -31.380476 -38.847958
      P+2 -12.681051 -14.661134 -13.688742 -16.829298 -20.320133
      P+3 -8.164744 -13.097990 -11.784309 -15.419610 -17.822252
      P+4 -0.023353 -0.926852 -8.036203 -14.583183 -17.071484
      P+5 0.022854 -0.037756 -0.002519 -1.891178 -7.795961






      python pandas






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 14 '18 at 3:40









      Andreas

      1,89731018




      1,89731018










      asked Nov 14 '18 at 3:19









      X.tangX.tang

      113




      113






















          2 Answers
          2






          active

          oldest

          votes


















          2














          By default, Pandas operations align data based on their index.
          So consider for example



          In [19]: df = pd.DataFrame([(10,1),(9,2),(8,3),(7,4)], index=list('ABDC'))

          In [20]: df
          Out[20]:
          0 1
          A 10 1
          B 9 2
          D 8 3
          C 7 4


          When Pandas evaluates df.apply(lambda x: x.sort_values()),
          it generates the Series:



          In [24]: df[0].sort_values()
          Out[24]:
          C 7
          D 8
          B 9
          A 10
          Name: 0, dtype: int64

          In [25]: df[1].sort_values()
          Out[25]:
          A 1
          B 2
          D 3
          C 4
          Name: 1, dtype: int64


          and then tries to combine these two Series into a resultant DataFrame. It does that by aligning the indices:



          In [21]: df.apply(lambda x: x.sort_values()) 
          Out[21]:
          0 1
          A 10 1
          B 9 2
          C 7 4
          D 8 3



          In contrast, when the lambda function returns a NumPy array there is no index to align upon. So Pandas merely pastes the values from the NumPy array into a resultant DataFrame in the same order.



          So, when Pandas evaluates df.apply(lambda x: x.sort_values().values),
          it generates the NumPy arrays:



          In [26]: df[0].sort_values().values
          Out[26]: array([ 7, 8, 9, 10])

          In [27]: df[1].sort_values().values
          Out[27]: array([1, 2, 3, 4])


          and then tries to combine these two NumPy arrays into a resultant DataFrame with the values in the same order



          In [28]: df.apply(lambda x: x.sort_values().values) 
          Out[28]:
          0 1
          A 7 1
          B 8 2
          D 9 3
          C 10 4





          share|improve this answer























          • @ unutbu . great explanation.

            – pygo
            Nov 14 '18 at 4:27











          • That is a correct and detailed answer, thank you very much !

            – X.tang
            Nov 14 '18 at 8:41











          • Then don't forget to accept it @X.tang

            – IanS
            Nov 22 '18 at 9:11


















          0














          Welcome to StackOverflow!



          Based on pandas documentation, sort_values() return the DataFrame object itself, while values() return the numpy array representation of the values in the DataFrame. Since apply() applies the specified function across the axis of the DataFrame, the applied function must return the numpy array representation of that current row/column, instead of returning the whole DataFrame. That is why it gives you the wrong result when you are only using sort_values().



          You can read the more complete explanation at sort_values() documentation, values() documentation, and apply() documentation






          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%2f53292709%2fwhy-sort-values-is-diifferent-form-sort-values-values%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









            2














            By default, Pandas operations align data based on their index.
            So consider for example



            In [19]: df = pd.DataFrame([(10,1),(9,2),(8,3),(7,4)], index=list('ABDC'))

            In [20]: df
            Out[20]:
            0 1
            A 10 1
            B 9 2
            D 8 3
            C 7 4


            When Pandas evaluates df.apply(lambda x: x.sort_values()),
            it generates the Series:



            In [24]: df[0].sort_values()
            Out[24]:
            C 7
            D 8
            B 9
            A 10
            Name: 0, dtype: int64

            In [25]: df[1].sort_values()
            Out[25]:
            A 1
            B 2
            D 3
            C 4
            Name: 1, dtype: int64


            and then tries to combine these two Series into a resultant DataFrame. It does that by aligning the indices:



            In [21]: df.apply(lambda x: x.sort_values()) 
            Out[21]:
            0 1
            A 10 1
            B 9 2
            C 7 4
            D 8 3



            In contrast, when the lambda function returns a NumPy array there is no index to align upon. So Pandas merely pastes the values from the NumPy array into a resultant DataFrame in the same order.



            So, when Pandas evaluates df.apply(lambda x: x.sort_values().values),
            it generates the NumPy arrays:



            In [26]: df[0].sort_values().values
            Out[26]: array([ 7, 8, 9, 10])

            In [27]: df[1].sort_values().values
            Out[27]: array([1, 2, 3, 4])


            and then tries to combine these two NumPy arrays into a resultant DataFrame with the values in the same order



            In [28]: df.apply(lambda x: x.sort_values().values) 
            Out[28]:
            0 1
            A 7 1
            B 8 2
            D 9 3
            C 10 4





            share|improve this answer























            • @ unutbu . great explanation.

              – pygo
              Nov 14 '18 at 4:27











            • That is a correct and detailed answer, thank you very much !

              – X.tang
              Nov 14 '18 at 8:41











            • Then don't forget to accept it @X.tang

              – IanS
              Nov 22 '18 at 9:11















            2














            By default, Pandas operations align data based on their index.
            So consider for example



            In [19]: df = pd.DataFrame([(10,1),(9,2),(8,3),(7,4)], index=list('ABDC'))

            In [20]: df
            Out[20]:
            0 1
            A 10 1
            B 9 2
            D 8 3
            C 7 4


            When Pandas evaluates df.apply(lambda x: x.sort_values()),
            it generates the Series:



            In [24]: df[0].sort_values()
            Out[24]:
            C 7
            D 8
            B 9
            A 10
            Name: 0, dtype: int64

            In [25]: df[1].sort_values()
            Out[25]:
            A 1
            B 2
            D 3
            C 4
            Name: 1, dtype: int64


            and then tries to combine these two Series into a resultant DataFrame. It does that by aligning the indices:



            In [21]: df.apply(lambda x: x.sort_values()) 
            Out[21]:
            0 1
            A 10 1
            B 9 2
            C 7 4
            D 8 3



            In contrast, when the lambda function returns a NumPy array there is no index to align upon. So Pandas merely pastes the values from the NumPy array into a resultant DataFrame in the same order.



            So, when Pandas evaluates df.apply(lambda x: x.sort_values().values),
            it generates the NumPy arrays:



            In [26]: df[0].sort_values().values
            Out[26]: array([ 7, 8, 9, 10])

            In [27]: df[1].sort_values().values
            Out[27]: array([1, 2, 3, 4])


            and then tries to combine these two NumPy arrays into a resultant DataFrame with the values in the same order



            In [28]: df.apply(lambda x: x.sort_values().values) 
            Out[28]:
            0 1
            A 7 1
            B 8 2
            D 9 3
            C 10 4





            share|improve this answer























            • @ unutbu . great explanation.

              – pygo
              Nov 14 '18 at 4:27











            • That is a correct and detailed answer, thank you very much !

              – X.tang
              Nov 14 '18 at 8:41











            • Then don't forget to accept it @X.tang

              – IanS
              Nov 22 '18 at 9:11













            2












            2








            2







            By default, Pandas operations align data based on their index.
            So consider for example



            In [19]: df = pd.DataFrame([(10,1),(9,2),(8,3),(7,4)], index=list('ABDC'))

            In [20]: df
            Out[20]:
            0 1
            A 10 1
            B 9 2
            D 8 3
            C 7 4


            When Pandas evaluates df.apply(lambda x: x.sort_values()),
            it generates the Series:



            In [24]: df[0].sort_values()
            Out[24]:
            C 7
            D 8
            B 9
            A 10
            Name: 0, dtype: int64

            In [25]: df[1].sort_values()
            Out[25]:
            A 1
            B 2
            D 3
            C 4
            Name: 1, dtype: int64


            and then tries to combine these two Series into a resultant DataFrame. It does that by aligning the indices:



            In [21]: df.apply(lambda x: x.sort_values()) 
            Out[21]:
            0 1
            A 10 1
            B 9 2
            C 7 4
            D 8 3



            In contrast, when the lambda function returns a NumPy array there is no index to align upon. So Pandas merely pastes the values from the NumPy array into a resultant DataFrame in the same order.



            So, when Pandas evaluates df.apply(lambda x: x.sort_values().values),
            it generates the NumPy arrays:



            In [26]: df[0].sort_values().values
            Out[26]: array([ 7, 8, 9, 10])

            In [27]: df[1].sort_values().values
            Out[27]: array([1, 2, 3, 4])


            and then tries to combine these two NumPy arrays into a resultant DataFrame with the values in the same order



            In [28]: df.apply(lambda x: x.sort_values().values) 
            Out[28]:
            0 1
            A 7 1
            B 8 2
            D 9 3
            C 10 4





            share|improve this answer













            By default, Pandas operations align data based on their index.
            So consider for example



            In [19]: df = pd.DataFrame([(10,1),(9,2),(8,3),(7,4)], index=list('ABDC'))

            In [20]: df
            Out[20]:
            0 1
            A 10 1
            B 9 2
            D 8 3
            C 7 4


            When Pandas evaluates df.apply(lambda x: x.sort_values()),
            it generates the Series:



            In [24]: df[0].sort_values()
            Out[24]:
            C 7
            D 8
            B 9
            A 10
            Name: 0, dtype: int64

            In [25]: df[1].sort_values()
            Out[25]:
            A 1
            B 2
            D 3
            C 4
            Name: 1, dtype: int64


            and then tries to combine these two Series into a resultant DataFrame. It does that by aligning the indices:



            In [21]: df.apply(lambda x: x.sort_values()) 
            Out[21]:
            0 1
            A 10 1
            B 9 2
            C 7 4
            D 8 3



            In contrast, when the lambda function returns a NumPy array there is no index to align upon. So Pandas merely pastes the values from the NumPy array into a resultant DataFrame in the same order.



            So, when Pandas evaluates df.apply(lambda x: x.sort_values().values),
            it generates the NumPy arrays:



            In [26]: df[0].sort_values().values
            Out[26]: array([ 7, 8, 9, 10])

            In [27]: df[1].sort_values().values
            Out[27]: array([1, 2, 3, 4])


            and then tries to combine these two NumPy arrays into a resultant DataFrame with the values in the same order



            In [28]: df.apply(lambda x: x.sort_values().values) 
            Out[28]:
            0 1
            A 7 1
            B 8 2
            D 9 3
            C 10 4






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 14 '18 at 3:36









            unutbuunutbu

            549k10111801237




            549k10111801237












            • @ unutbu . great explanation.

              – pygo
              Nov 14 '18 at 4:27











            • That is a correct and detailed answer, thank you very much !

              – X.tang
              Nov 14 '18 at 8:41











            • Then don't forget to accept it @X.tang

              – IanS
              Nov 22 '18 at 9:11

















            • @ unutbu . great explanation.

              – pygo
              Nov 14 '18 at 4:27











            • That is a correct and detailed answer, thank you very much !

              – X.tang
              Nov 14 '18 at 8:41











            • Then don't forget to accept it @X.tang

              – IanS
              Nov 22 '18 at 9:11
















            @ unutbu . great explanation.

            – pygo
            Nov 14 '18 at 4:27





            @ unutbu . great explanation.

            – pygo
            Nov 14 '18 at 4:27













            That is a correct and detailed answer, thank you very much !

            – X.tang
            Nov 14 '18 at 8:41





            That is a correct and detailed answer, thank you very much !

            – X.tang
            Nov 14 '18 at 8:41













            Then don't forget to accept it @X.tang

            – IanS
            Nov 22 '18 at 9:11





            Then don't forget to accept it @X.tang

            – IanS
            Nov 22 '18 at 9:11













            0














            Welcome to StackOverflow!



            Based on pandas documentation, sort_values() return the DataFrame object itself, while values() return the numpy array representation of the values in the DataFrame. Since apply() applies the specified function across the axis of the DataFrame, the applied function must return the numpy array representation of that current row/column, instead of returning the whole DataFrame. That is why it gives you the wrong result when you are only using sort_values().



            You can read the more complete explanation at sort_values() documentation, values() documentation, and apply() documentation






            share|improve this answer



























              0














              Welcome to StackOverflow!



              Based on pandas documentation, sort_values() return the DataFrame object itself, while values() return the numpy array representation of the values in the DataFrame. Since apply() applies the specified function across the axis of the DataFrame, the applied function must return the numpy array representation of that current row/column, instead of returning the whole DataFrame. That is why it gives you the wrong result when you are only using sort_values().



              You can read the more complete explanation at sort_values() documentation, values() documentation, and apply() documentation






              share|improve this answer

























                0












                0








                0







                Welcome to StackOverflow!



                Based on pandas documentation, sort_values() return the DataFrame object itself, while values() return the numpy array representation of the values in the DataFrame. Since apply() applies the specified function across the axis of the DataFrame, the applied function must return the numpy array representation of that current row/column, instead of returning the whole DataFrame. That is why it gives you the wrong result when you are only using sort_values().



                You can read the more complete explanation at sort_values() documentation, values() documentation, and apply() documentation






                share|improve this answer













                Welcome to StackOverflow!



                Based on pandas documentation, sort_values() return the DataFrame object itself, while values() return the numpy array representation of the values in the DataFrame. Since apply() applies the specified function across the axis of the DataFrame, the applied function must return the numpy array representation of that current row/column, instead of returning the whole DataFrame. That is why it gives you the wrong result when you are only using sort_values().



                You can read the more complete explanation at sort_values() documentation, values() documentation, and apply() documentation







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 14 '18 at 3:26









                AndreasAndreas

                1,89731018




                1,89731018



























                    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%2f53292709%2fwhy-sort-values-is-diifferent-form-sort-values-values%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







                    這個網誌中的熱門文章

                    How to read a connectionString WITH PROVIDER in .NET Core?

                    Node.js Script on GitHub Pages or Amazon S3

                    Museum of Modern and Contemporary Art of Trento and Rovereto