Pandas Series difference between accessing values using string and nested list

Multi tool use
Multi tool use












4















Hi I'm still new to pandas method and I just to have a better understanding about the pd.Series



ser = pd.Series(data=[100,"200",300,"400",500],index=["tom","bob","nancy","dan","eric"])


I found out when ever I want to extract a value using the index of it for example



ser["nancy"]


the output will be



300


but if i use nested list



ser[["nancy"]]


I will get



nancy    300


I know it's maybe a simple thing but I just want to know the reason behind that to have a better understanding
and if there is any resource you would recommend I would highly appreciate it



Thanks










share|improve this question





























    4















    Hi I'm still new to pandas method and I just to have a better understanding about the pd.Series



    ser = pd.Series(data=[100,"200",300,"400",500],index=["tom","bob","nancy","dan","eric"])


    I found out when ever I want to extract a value using the index of it for example



    ser["nancy"]


    the output will be



    300


    but if i use nested list



    ser[["nancy"]]


    I will get



    nancy    300


    I know it's maybe a simple thing but I just want to know the reason behind that to have a better understanding
    and if there is any resource you would recommend I would highly appreciate it



    Thanks










    share|improve this question



























      4












      4








      4








      Hi I'm still new to pandas method and I just to have a better understanding about the pd.Series



      ser = pd.Series(data=[100,"200",300,"400",500],index=["tom","bob","nancy","dan","eric"])


      I found out when ever I want to extract a value using the index of it for example



      ser["nancy"]


      the output will be



      300


      but if i use nested list



      ser[["nancy"]]


      I will get



      nancy    300


      I know it's maybe a simple thing but I just want to know the reason behind that to have a better understanding
      and if there is any resource you would recommend I would highly appreciate it



      Thanks










      share|improve this question
















      Hi I'm still new to pandas method and I just to have a better understanding about the pd.Series



      ser = pd.Series(data=[100,"200",300,"400",500],index=["tom","bob","nancy","dan","eric"])


      I found out when ever I want to extract a value using the index of it for example



      ser["nancy"]


      the output will be



      300


      but if i use nested list



      ser[["nancy"]]


      I will get



      nancy    300


      I know it's maybe a simple thing but I just want to know the reason behind that to have a better understanding
      and if there is any resource you would recommend I would highly appreciate it



      Thanks







      python pandas series






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 7 at 16:33









      Mohit Motwani

      1,7131522




      1,7131522










      asked Jan 1 at 12:24









      Khaled KhanfarKhaled Khanfar

      354




      354
























          2 Answers
          2






          active

          oldest

          votes


















          4














          When you use:



          ser["nancy"]
          >> 300


          it returns an integer



          type(ser['nancy']
          >> int


          But when you use



          ser[['nancy']]
          >> nancy 300
          dtype: object


          It actually returns a series:



          type(ser[['nancy']])
          >> pandas.core.series.Series


          So when you use a list, it always returns a series and not just the corresponding value as when you use just a string:



          ser[["nancy", "dan"]]
          >> nancy 300
          dan 400
          dtype: object





          share|improve this answer

































            3














            When you do ser["nancy"] you signal pandas you want a single value, so it will return the value at the "nancy" index key. When you do ser[["nancy"]] you signal pandas you may want multiple values, so it returns a data structure (in this case a Series) that contains those (possible) multiple values. This is better illustrated in the following example:



            import pandas as pd

            ser = pd.Series(data=[100, "200", 300, "400", 500], index=["tom", "bob", "nancy", "dan", "eric"])

            result = ser["nancy"]
            print(result, type(result))

            result = ser[["nancy", "dan"]]
            print(result, type(result))


            Output



            300 <class 'int'>
            nancy 300
            dan 400
            dtype: object <class 'pandas.core.series.Series'>


            As you can see from the output, ser["nancy"] return the value of ser at "nancy", in this case 300, you can further verify that is a single value by it's type (int). For the second case the type of result is a Series.






            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%2f53995406%2fpandas-series-difference-between-accessing-values-using-string-and-nested-list%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









              4














              When you use:



              ser["nancy"]
              >> 300


              it returns an integer



              type(ser['nancy']
              >> int


              But when you use



              ser[['nancy']]
              >> nancy 300
              dtype: object


              It actually returns a series:



              type(ser[['nancy']])
              >> pandas.core.series.Series


              So when you use a list, it always returns a series and not just the corresponding value as when you use just a string:



              ser[["nancy", "dan"]]
              >> nancy 300
              dan 400
              dtype: object





              share|improve this answer






























                4














                When you use:



                ser["nancy"]
                >> 300


                it returns an integer



                type(ser['nancy']
                >> int


                But when you use



                ser[['nancy']]
                >> nancy 300
                dtype: object


                It actually returns a series:



                type(ser[['nancy']])
                >> pandas.core.series.Series


                So when you use a list, it always returns a series and not just the corresponding value as when you use just a string:



                ser[["nancy", "dan"]]
                >> nancy 300
                dan 400
                dtype: object





                share|improve this answer




























                  4












                  4








                  4







                  When you use:



                  ser["nancy"]
                  >> 300


                  it returns an integer



                  type(ser['nancy']
                  >> int


                  But when you use



                  ser[['nancy']]
                  >> nancy 300
                  dtype: object


                  It actually returns a series:



                  type(ser[['nancy']])
                  >> pandas.core.series.Series


                  So when you use a list, it always returns a series and not just the corresponding value as when you use just a string:



                  ser[["nancy", "dan"]]
                  >> nancy 300
                  dan 400
                  dtype: object





                  share|improve this answer















                  When you use:



                  ser["nancy"]
                  >> 300


                  it returns an integer



                  type(ser['nancy']
                  >> int


                  But when you use



                  ser[['nancy']]
                  >> nancy 300
                  dtype: object


                  It actually returns a series:



                  type(ser[['nancy']])
                  >> pandas.core.series.Series


                  So when you use a list, it always returns a series and not just the corresponding value as when you use just a string:



                  ser[["nancy", "dan"]]
                  >> nancy 300
                  dan 400
                  dtype: object






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Jan 2 at 10:24

























                  answered Jan 1 at 12:34









                  Mohit MotwaniMohit Motwani

                  1,7131522




                  1,7131522

























                      3














                      When you do ser["nancy"] you signal pandas you want a single value, so it will return the value at the "nancy" index key. When you do ser[["nancy"]] you signal pandas you may want multiple values, so it returns a data structure (in this case a Series) that contains those (possible) multiple values. This is better illustrated in the following example:



                      import pandas as pd

                      ser = pd.Series(data=[100, "200", 300, "400", 500], index=["tom", "bob", "nancy", "dan", "eric"])

                      result = ser["nancy"]
                      print(result, type(result))

                      result = ser[["nancy", "dan"]]
                      print(result, type(result))


                      Output



                      300 <class 'int'>
                      nancy 300
                      dan 400
                      dtype: object <class 'pandas.core.series.Series'>


                      As you can see from the output, ser["nancy"] return the value of ser at "nancy", in this case 300, you can further verify that is a single value by it's type (int). For the second case the type of result is a Series.






                      share|improve this answer




























                        3














                        When you do ser["nancy"] you signal pandas you want a single value, so it will return the value at the "nancy" index key. When you do ser[["nancy"]] you signal pandas you may want multiple values, so it returns a data structure (in this case a Series) that contains those (possible) multiple values. This is better illustrated in the following example:



                        import pandas as pd

                        ser = pd.Series(data=[100, "200", 300, "400", 500], index=["tom", "bob", "nancy", "dan", "eric"])

                        result = ser["nancy"]
                        print(result, type(result))

                        result = ser[["nancy", "dan"]]
                        print(result, type(result))


                        Output



                        300 <class 'int'>
                        nancy 300
                        dan 400
                        dtype: object <class 'pandas.core.series.Series'>


                        As you can see from the output, ser["nancy"] return the value of ser at "nancy", in this case 300, you can further verify that is a single value by it's type (int). For the second case the type of result is a Series.






                        share|improve this answer


























                          3












                          3








                          3







                          When you do ser["nancy"] you signal pandas you want a single value, so it will return the value at the "nancy" index key. When you do ser[["nancy"]] you signal pandas you may want multiple values, so it returns a data structure (in this case a Series) that contains those (possible) multiple values. This is better illustrated in the following example:



                          import pandas as pd

                          ser = pd.Series(data=[100, "200", 300, "400", 500], index=["tom", "bob", "nancy", "dan", "eric"])

                          result = ser["nancy"]
                          print(result, type(result))

                          result = ser[["nancy", "dan"]]
                          print(result, type(result))


                          Output



                          300 <class 'int'>
                          nancy 300
                          dan 400
                          dtype: object <class 'pandas.core.series.Series'>


                          As you can see from the output, ser["nancy"] return the value of ser at "nancy", in this case 300, you can further verify that is a single value by it's type (int). For the second case the type of result is a Series.






                          share|improve this answer













                          When you do ser["nancy"] you signal pandas you want a single value, so it will return the value at the "nancy" index key. When you do ser[["nancy"]] you signal pandas you may want multiple values, so it returns a data structure (in this case a Series) that contains those (possible) multiple values. This is better illustrated in the following example:



                          import pandas as pd

                          ser = pd.Series(data=[100, "200", 300, "400", 500], index=["tom", "bob", "nancy", "dan", "eric"])

                          result = ser["nancy"]
                          print(result, type(result))

                          result = ser[["nancy", "dan"]]
                          print(result, type(result))


                          Output



                          300 <class 'int'>
                          nancy 300
                          dan 400
                          dtype: object <class 'pandas.core.series.Series'>


                          As you can see from the output, ser["nancy"] return the value of ser at "nancy", in this case 300, you can further verify that is a single value by it's type (int). For the second case the type of result is a Series.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Jan 1 at 12:32









                          Daniel MesejoDaniel Mesejo

                          18.6k21432




                          18.6k21432






























                              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%2f53995406%2fpandas-series-difference-between-accessing-values-using-string-and-nested-list%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







                              uEQ skyu,hCc1wA0dzSZNW72aje6tSfa1GkFdwX,HN3ddZ6F,dKxd3F,qFz2 pBjBp1WIIeOuPx zo XMPb
                              OlofK7,8tWfB9LaOpHj6womu,W7WxxA6iXZ,DcDS ZMcD zc,8zhcvB6R2VtZ00jfbf,5sbhfoC0ewCwoXwesrr5qMIT05

                              Popular posts from this blog

                              Monofisismo

                              Angular Downloading a file using contenturl with Basic Authentication

                              Olmecas