pandas convert lists in multiple columns within DataFrame to separate columns











up vote
0
down vote

favorite












I am trying to convert a list within multiple columns of a pandas DataFrame into separate columns.



Say, I have a dataframe like this:



           0          1
0 [1, 2, 3] [4, 5, 6]
1 [1, 2, 3] [4, 5, 6]
2 [1, 2, 3] [4, 5, 6]


And would like to convert it to something like this:



   0  1  2  0  1  2
0 1 2 3 4 5 6
1 1 2 3 4 5 6
2 1 2 3 4 5 6


I have managed to do this in a loop. However, I would like to do this in fewer lines.
My code snippet so far is as follows:



import pandas as pd

df = pd.DataFrame([[[1,2,3],[4,5,6]],[[1,2,3],[4,5,6]],[[1,2,3],[4,5,6]]])
output1 = df[0].apply(pd.Series)
output2 = df[1].apply(pd.Series)

output = pd.concat([output1, output2], axis=1)









share|improve this question






















  • Are you sure you want that output? Duplicate column names are allowed but not particularly useful...
    – Jon Clements
    Nov 22 at 15:13










  • At this point I am not really worried about the duplicated column names. I would like to reduce it to one line instead of implementing what will be loop/concat structure once applied to a dataframe with more than two columns
    – Christian
    Nov 22 at 15:18















up vote
0
down vote

favorite












I am trying to convert a list within multiple columns of a pandas DataFrame into separate columns.



Say, I have a dataframe like this:



           0          1
0 [1, 2, 3] [4, 5, 6]
1 [1, 2, 3] [4, 5, 6]
2 [1, 2, 3] [4, 5, 6]


And would like to convert it to something like this:



   0  1  2  0  1  2
0 1 2 3 4 5 6
1 1 2 3 4 5 6
2 1 2 3 4 5 6


I have managed to do this in a loop. However, I would like to do this in fewer lines.
My code snippet so far is as follows:



import pandas as pd

df = pd.DataFrame([[[1,2,3],[4,5,6]],[[1,2,3],[4,5,6]],[[1,2,3],[4,5,6]]])
output1 = df[0].apply(pd.Series)
output2 = df[1].apply(pd.Series)

output = pd.concat([output1, output2], axis=1)









share|improve this question






















  • Are you sure you want that output? Duplicate column names are allowed but not particularly useful...
    – Jon Clements
    Nov 22 at 15:13










  • At this point I am not really worried about the duplicated column names. I would like to reduce it to one line instead of implementing what will be loop/concat structure once applied to a dataframe with more than two columns
    – Christian
    Nov 22 at 15:18













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am trying to convert a list within multiple columns of a pandas DataFrame into separate columns.



Say, I have a dataframe like this:



           0          1
0 [1, 2, 3] [4, 5, 6]
1 [1, 2, 3] [4, 5, 6]
2 [1, 2, 3] [4, 5, 6]


And would like to convert it to something like this:



   0  1  2  0  1  2
0 1 2 3 4 5 6
1 1 2 3 4 5 6
2 1 2 3 4 5 6


I have managed to do this in a loop. However, I would like to do this in fewer lines.
My code snippet so far is as follows:



import pandas as pd

df = pd.DataFrame([[[1,2,3],[4,5,6]],[[1,2,3],[4,5,6]],[[1,2,3],[4,5,6]]])
output1 = df[0].apply(pd.Series)
output2 = df[1].apply(pd.Series)

output = pd.concat([output1, output2], axis=1)









share|improve this question













I am trying to convert a list within multiple columns of a pandas DataFrame into separate columns.



Say, I have a dataframe like this:



           0          1
0 [1, 2, 3] [4, 5, 6]
1 [1, 2, 3] [4, 5, 6]
2 [1, 2, 3] [4, 5, 6]


And would like to convert it to something like this:



   0  1  2  0  1  2
0 1 2 3 4 5 6
1 1 2 3 4 5 6
2 1 2 3 4 5 6


I have managed to do this in a loop. However, I would like to do this in fewer lines.
My code snippet so far is as follows:



import pandas as pd

df = pd.DataFrame([[[1,2,3],[4,5,6]],[[1,2,3],[4,5,6]],[[1,2,3],[4,5,6]]])
output1 = df[0].apply(pd.Series)
output2 = df[1].apply(pd.Series)

output = pd.concat([output1, output2], axis=1)






pandas dataframe apply






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 at 15:08









Christian

14518




14518












  • Are you sure you want that output? Duplicate column names are allowed but not particularly useful...
    – Jon Clements
    Nov 22 at 15:13










  • At this point I am not really worried about the duplicated column names. I would like to reduce it to one line instead of implementing what will be loop/concat structure once applied to a dataframe with more than two columns
    – Christian
    Nov 22 at 15:18


















  • Are you sure you want that output? Duplicate column names are allowed but not particularly useful...
    – Jon Clements
    Nov 22 at 15:13










  • At this point I am not really worried about the duplicated column names. I would like to reduce it to one line instead of implementing what will be loop/concat structure once applied to a dataframe with more than two columns
    – Christian
    Nov 22 at 15:18
















Are you sure you want that output? Duplicate column names are allowed but not particularly useful...
– Jon Clements
Nov 22 at 15:13




Are you sure you want that output? Duplicate column names are allowed but not particularly useful...
– Jon Clements
Nov 22 at 15:13












At this point I am not really worried about the duplicated column names. I would like to reduce it to one line instead of implementing what will be loop/concat structure once applied to a dataframe with more than two columns
– Christian
Nov 22 at 15:18




At this point I am not really worried about the duplicated column names. I would like to reduce it to one line instead of implementing what will be loop/concat structure once applied to a dataframe with more than two columns
– Christian
Nov 22 at 15:18












2 Answers
2






active

oldest

votes

















up vote
2
down vote



accepted










If you don't care about the column names you could do:



>>> df.apply(np.hstack, axis=1).apply(pd.Series)
0 1 2 3 4 5
0 1 2 3 4 5 6
1 1 2 3 4 5 6
2 1 2 3 4 5 6





share|improve this answer




























    up vote
    1
    down vote













    Using sum



    pd.DataFrame(df.sum(1).tolist())
    0 1 2 3 4 5
    0 1 2 3 4 5 6
    1 1 2 3 4 5 6
    2 1 2 3 4 5 6





    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
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53433790%2fpandas-convert-lists-in-multiple-columns-within-dataframe-to-separate-columns%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








      up vote
      2
      down vote



      accepted










      If you don't care about the column names you could do:



      >>> df.apply(np.hstack, axis=1).apply(pd.Series)
      0 1 2 3 4 5
      0 1 2 3 4 5 6
      1 1 2 3 4 5 6
      2 1 2 3 4 5 6





      share|improve this answer

























        up vote
        2
        down vote



        accepted










        If you don't care about the column names you could do:



        >>> df.apply(np.hstack, axis=1).apply(pd.Series)
        0 1 2 3 4 5
        0 1 2 3 4 5 6
        1 1 2 3 4 5 6
        2 1 2 3 4 5 6





        share|improve this answer























          up vote
          2
          down vote



          accepted







          up vote
          2
          down vote



          accepted






          If you don't care about the column names you could do:



          >>> df.apply(np.hstack, axis=1).apply(pd.Series)
          0 1 2 3 4 5
          0 1 2 3 4 5 6
          1 1 2 3 4 5 6
          2 1 2 3 4 5 6





          share|improve this answer












          If you don't care about the column names you could do:



          >>> df.apply(np.hstack, axis=1).apply(pd.Series)
          0 1 2 3 4 5
          0 1 2 3 4 5 6
          1 1 2 3 4 5 6
          2 1 2 3 4 5 6






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 22 at 15:34









          w-m

          5,9992233




          5,9992233
























              up vote
              1
              down vote













              Using sum



              pd.DataFrame(df.sum(1).tolist())
              0 1 2 3 4 5
              0 1 2 3 4 5 6
              1 1 2 3 4 5 6
              2 1 2 3 4 5 6





              share|improve this answer

























                up vote
                1
                down vote













                Using sum



                pd.DataFrame(df.sum(1).tolist())
                0 1 2 3 4 5
                0 1 2 3 4 5 6
                1 1 2 3 4 5 6
                2 1 2 3 4 5 6





                share|improve this answer























                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  Using sum



                  pd.DataFrame(df.sum(1).tolist())
                  0 1 2 3 4 5
                  0 1 2 3 4 5 6
                  1 1 2 3 4 5 6
                  2 1 2 3 4 5 6





                  share|improve this answer












                  Using sum



                  pd.DataFrame(df.sum(1).tolist())
                  0 1 2 3 4 5
                  0 1 2 3 4 5 6
                  1 1 2 3 4 5 6
                  2 1 2 3 4 5 6






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 22 at 15:50









                  W-B

                  97.1k73162




                  97.1k73162






























                      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.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • 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%2f53433790%2fpandas-convert-lists-in-multiple-columns-within-dataframe-to-separate-columns%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

                      Trompette piccolo

                      Slow SSRS Report in dynamic grouping and multiple parameters

                      Simon Yates (cyclisme)