Convert list to column in Python Dataframe



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I have a dataframe df which looks like this:



CustomerId Age
1 25
2 18
3 45
4 57
5 34


I have a list called "Price" which looks like this:



Price = [123,345,1212,11,677]


I want to add that list to the dataframe. Here is my code:



df['Price'] = Price


It seems to work but when I print the dataframe the field called "Price" contains all the metadata information such as Name, Type... as well as the value of the Price.



How can I create a column called "Price" containing only the values of the Price list so that the dataframe looks like:



CustomerId Age Price
1 25 123
2 18 345
3 45 1212
4 57 11
5 34 677









share|improve this question



















  • 5





    Please include your code, otherwise you are expecting people to guess what you wrote so that they can guess where you made an error.

    – Dragonthoughts
    Jul 6 '18 at 11:55











  • What solution was the one that worked???

    – Adrian
    Mar 30 at 14:58

















0















I have a dataframe df which looks like this:



CustomerId Age
1 25
2 18
3 45
4 57
5 34


I have a list called "Price" which looks like this:



Price = [123,345,1212,11,677]


I want to add that list to the dataframe. Here is my code:



df['Price'] = Price


It seems to work but when I print the dataframe the field called "Price" contains all the metadata information such as Name, Type... as well as the value of the Price.



How can I create a column called "Price" containing only the values of the Price list so that the dataframe looks like:



CustomerId Age Price
1 25 123
2 18 345
3 45 1212
4 57 11
5 34 677









share|improve this question



















  • 5





    Please include your code, otherwise you are expecting people to guess what you wrote so that they can guess where you made an error.

    – Dragonthoughts
    Jul 6 '18 at 11:55











  • What solution was the one that worked???

    – Adrian
    Mar 30 at 14:58













0












0








0








I have a dataframe df which looks like this:



CustomerId Age
1 25
2 18
3 45
4 57
5 34


I have a list called "Price" which looks like this:



Price = [123,345,1212,11,677]


I want to add that list to the dataframe. Here is my code:



df['Price'] = Price


It seems to work but when I print the dataframe the field called "Price" contains all the metadata information such as Name, Type... as well as the value of the Price.



How can I create a column called "Price" containing only the values of the Price list so that the dataframe looks like:



CustomerId Age Price
1 25 123
2 18 345
3 45 1212
4 57 11
5 34 677









share|improve this question
















I have a dataframe df which looks like this:



CustomerId Age
1 25
2 18
3 45
4 57
5 34


I have a list called "Price" which looks like this:



Price = [123,345,1212,11,677]


I want to add that list to the dataframe. Here is my code:



df['Price'] = Price


It seems to work but when I print the dataframe the field called "Price" contains all the metadata information such as Name, Type... as well as the value of the Price.



How can I create a column called "Price" containing only the values of the Price list so that the dataframe looks like:



CustomerId Age Price
1 25 123
2 18 345
3 45 1212
4 57 11
5 34 677






python list pandas dataframe metadata






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jul 6 '18 at 12:52









Azat Ibrakov

4,47371732




4,47371732










asked Jul 6 '18 at 11:54









Giampaolo LevoratoGiampaolo Levorato

3715




3715







  • 5





    Please include your code, otherwise you are expecting people to guess what you wrote so that they can guess where you made an error.

    – Dragonthoughts
    Jul 6 '18 at 11:55











  • What solution was the one that worked???

    – Adrian
    Mar 30 at 14:58












  • 5





    Please include your code, otherwise you are expecting people to guess what you wrote so that they can guess where you made an error.

    – Dragonthoughts
    Jul 6 '18 at 11:55











  • What solution was the one that worked???

    – Adrian
    Mar 30 at 14:58







5




5





Please include your code, otherwise you are expecting people to guess what you wrote so that they can guess where you made an error.

– Dragonthoughts
Jul 6 '18 at 11:55





Please include your code, otherwise you are expecting people to guess what you wrote so that they can guess where you made an error.

– Dragonthoughts
Jul 6 '18 at 11:55













What solution was the one that worked???

– Adrian
Mar 30 at 14:58





What solution was the one that worked???

– Adrian
Mar 30 at 14:58












4 Answers
4






active

oldest

votes


















2














In my Opinion, the most elegant solution is to use assign:



df.assign(Price=Price)
CustomerId Age Price
1 25 123
2 18 345
3 45 1212
4 57 11
5 34 677


note that assign actually returns a DataFrame.
Assign creates a new Column 'Price' (left Price) with the content of the list Price (right Price)






share|improve this answer
































    0














    You can add pandas series as column.



    import pandas as pd
    df['Price'] = pd.Series(Price)





    share|improve this answer






























      0














      import pandas as pd
      df['Price'] = pd.Series(Price)


      if you use this you will not get the error if you have less values in the series than to your dataframe otherwise you will get the error that will tell you have less vales in the list and that can not be appended.






      share|improve this answer






























        0














        I copy pasted your example into a dataframe using pandas.read_clipboard and then added the column like this:



        import pandas as pd
        df = pd.read_clipboard()
        Price = [123,345,1212,11,677]
        df.loc[:,'Price'] = Price
        df


        Generating this:



        CustomerId Age Price
        0 1 25 123
        1 2 18 345
        2 3 45 1212
        3 4 57 11
        4 5 34 677





        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%2f51209933%2fconvert-list-to-column-in-python-dataframe%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          4 Answers
          4






          active

          oldest

          votes








          4 Answers
          4






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          In my Opinion, the most elegant solution is to use assign:



          df.assign(Price=Price)
          CustomerId Age Price
          1 25 123
          2 18 345
          3 45 1212
          4 57 11
          5 34 677


          note that assign actually returns a DataFrame.
          Assign creates a new Column 'Price' (left Price) with the content of the list Price (right Price)






          share|improve this answer





























            2














            In my Opinion, the most elegant solution is to use assign:



            df.assign(Price=Price)
            CustomerId Age Price
            1 25 123
            2 18 345
            3 45 1212
            4 57 11
            5 34 677


            note that assign actually returns a DataFrame.
            Assign creates a new Column 'Price' (left Price) with the content of the list Price (right Price)






            share|improve this answer



























              2












              2








              2







              In my Opinion, the most elegant solution is to use assign:



              df.assign(Price=Price)
              CustomerId Age Price
              1 25 123
              2 18 345
              3 45 1212
              4 57 11
              5 34 677


              note that assign actually returns a DataFrame.
              Assign creates a new Column 'Price' (left Price) with the content of the list Price (right Price)






              share|improve this answer















              In my Opinion, the most elegant solution is to use assign:



              df.assign(Price=Price)
              CustomerId Age Price
              1 25 123
              2 18 345
              3 45 1212
              4 57 11
              5 34 677


              note that assign actually returns a DataFrame.
              Assign creates a new Column 'Price' (left Price) with the content of the list Price (right Price)







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Jul 6 '18 at 12:51

























              answered Jul 6 '18 at 12:45









              Quickbeam2k1Quickbeam2k1

              2,3271128




              2,3271128























                  0














                  You can add pandas series as column.



                  import pandas as pd
                  df['Price'] = pd.Series(Price)





                  share|improve this answer



























                    0














                    You can add pandas series as column.



                    import pandas as pd
                    df['Price'] = pd.Series(Price)





                    share|improve this answer

























                      0












                      0








                      0







                      You can add pandas series as column.



                      import pandas as pd
                      df['Price'] = pd.Series(Price)





                      share|improve this answer













                      You can add pandas series as column.



                      import pandas as pd
                      df['Price'] = pd.Series(Price)






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Jul 6 '18 at 11:59









                      hamza tunahamza tuna

                      9191612




                      9191612





















                          0














                          import pandas as pd
                          df['Price'] = pd.Series(Price)


                          if you use this you will not get the error if you have less values in the series than to your dataframe otherwise you will get the error that will tell you have less vales in the list and that can not be appended.






                          share|improve this answer



























                            0














                            import pandas as pd
                            df['Price'] = pd.Series(Price)


                            if you use this you will not get the error if you have less values in the series than to your dataframe otherwise you will get the error that will tell you have less vales in the list and that can not be appended.






                            share|improve this answer

























                              0












                              0








                              0







                              import pandas as pd
                              df['Price'] = pd.Series(Price)


                              if you use this you will not get the error if you have less values in the series than to your dataframe otherwise you will get the error that will tell you have less vales in the list and that can not be appended.






                              share|improve this answer













                              import pandas as pd
                              df['Price'] = pd.Series(Price)


                              if you use this you will not get the error if you have less values in the series than to your dataframe otherwise you will get the error that will tell you have less vales in the list and that can not be appended.







                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Nov 14 '18 at 11:26









                              akshit0106akshit0106

                              142




                              142





















                                  0














                                  I copy pasted your example into a dataframe using pandas.read_clipboard and then added the column like this:



                                  import pandas as pd
                                  df = pd.read_clipboard()
                                  Price = [123,345,1212,11,677]
                                  df.loc[:,'Price'] = Price
                                  df


                                  Generating this:



                                  CustomerId Age Price
                                  0 1 25 123
                                  1 2 18 345
                                  2 3 45 1212
                                  3 4 57 11
                                  4 5 34 677





                                  share|improve this answer



























                                    0














                                    I copy pasted your example into a dataframe using pandas.read_clipboard and then added the column like this:



                                    import pandas as pd
                                    df = pd.read_clipboard()
                                    Price = [123,345,1212,11,677]
                                    df.loc[:,'Price'] = Price
                                    df


                                    Generating this:



                                    CustomerId Age Price
                                    0 1 25 123
                                    1 2 18 345
                                    2 3 45 1212
                                    3 4 57 11
                                    4 5 34 677





                                    share|improve this answer

























                                      0












                                      0








                                      0







                                      I copy pasted your example into a dataframe using pandas.read_clipboard and then added the column like this:



                                      import pandas as pd
                                      df = pd.read_clipboard()
                                      Price = [123,345,1212,11,677]
                                      df.loc[:,'Price'] = Price
                                      df


                                      Generating this:



                                      CustomerId Age Price
                                      0 1 25 123
                                      1 2 18 345
                                      2 3 45 1212
                                      3 4 57 11
                                      4 5 34 677





                                      share|improve this answer













                                      I copy pasted your example into a dataframe using pandas.read_clipboard and then added the column like this:



                                      import pandas as pd
                                      df = pd.read_clipboard()
                                      Price = [123,345,1212,11,677]
                                      df.loc[:,'Price'] = Price
                                      df


                                      Generating this:



                                      CustomerId Age Price
                                      0 1 25 123
                                      1 2 18 345
                                      2 3 45 1212
                                      3 4 57 11
                                      4 5 34 677






                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Nov 14 '18 at 11:33









                                      JorgeJorge

                                      1,47611022




                                      1,47611022



























                                          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%2f51209933%2fconvert-list-to-column-in-python-dataframe%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

                                          𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

                                          How do I collapse sections of code in Visual Studio Code for Windows?

                                          ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ