Convert a list of lists into a Pandas Dataframe












10












$begingroup$


I am trying to convert a list of lists which looks like the following into a Pandas Dataframe



[['New York Yankees ', '"Acevedo Juan"  ', 900000, ' Pitchern'], 
['New York Yankees ', '"Anderson Jason"', 300000, ' Pitchern'],
['New York Yankees ', '"Clemens Roger" ', 10100000, ' Pitchern'],
['New York Yankees ', '"Contreras Jose"', 5500000, ' Pitchern']]


I am basically trying to convert each item in the array into a pandas data frame which has four columns. What would be the best approach to this as pd.Dataframe does not quite give me what I am looking for.










share|improve this question











$endgroup$












  • $begingroup$
    see this question in stack overflow: stackoverflow.com/questions/.../…
    $endgroup$
    – keramat
    Jan 5 '18 at 18:46
















10












$begingroup$


I am trying to convert a list of lists which looks like the following into a Pandas Dataframe



[['New York Yankees ', '"Acevedo Juan"  ', 900000, ' Pitchern'], 
['New York Yankees ', '"Anderson Jason"', 300000, ' Pitchern'],
['New York Yankees ', '"Clemens Roger" ', 10100000, ' Pitchern'],
['New York Yankees ', '"Contreras Jose"', 5500000, ' Pitchern']]


I am basically trying to convert each item in the array into a pandas data frame which has four columns. What would be the best approach to this as pd.Dataframe does not quite give me what I am looking for.










share|improve this question











$endgroup$












  • $begingroup$
    see this question in stack overflow: stackoverflow.com/questions/.../…
    $endgroup$
    – keramat
    Jan 5 '18 at 18:46














10












10








10


3



$begingroup$


I am trying to convert a list of lists which looks like the following into a Pandas Dataframe



[['New York Yankees ', '"Acevedo Juan"  ', 900000, ' Pitchern'], 
['New York Yankees ', '"Anderson Jason"', 300000, ' Pitchern'],
['New York Yankees ', '"Clemens Roger" ', 10100000, ' Pitchern'],
['New York Yankees ', '"Contreras Jose"', 5500000, ' Pitchern']]


I am basically trying to convert each item in the array into a pandas data frame which has four columns. What would be the best approach to this as pd.Dataframe does not quite give me what I am looking for.










share|improve this question











$endgroup$




I am trying to convert a list of lists which looks like the following into a Pandas Dataframe



[['New York Yankees ', '"Acevedo Juan"  ', 900000, ' Pitchern'], 
['New York Yankees ', '"Anderson Jason"', 300000, ' Pitchern'],
['New York Yankees ', '"Clemens Roger" ', 10100000, ' Pitchern'],
['New York Yankees ', '"Contreras Jose"', 5500000, ' Pitchern']]


I am basically trying to convert each item in the array into a pandas data frame which has four columns. What would be the best approach to this as pd.Dataframe does not quite give me what I am looking for.







pandas






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jun 5 '18 at 18:51









Emre

8,54011935




8,54011935










asked Jan 5 '18 at 18:40









Aravind VeluchamyAravind Veluchamy

51113




51113












  • $begingroup$
    see this question in stack overflow: stackoverflow.com/questions/.../…
    $endgroup$
    – keramat
    Jan 5 '18 at 18:46


















  • $begingroup$
    see this question in stack overflow: stackoverflow.com/questions/.../…
    $endgroup$
    – keramat
    Jan 5 '18 at 18:46
















$begingroup$
see this question in stack overflow: stackoverflow.com/questions/.../…
$endgroup$
– keramat
Jan 5 '18 at 18:46




$begingroup$
see this question in stack overflow: stackoverflow.com/questions/.../…
$endgroup$
– keramat
Jan 5 '18 at 18:46










3 Answers
3






active

oldest

votes


















13












$begingroup$

from pandas import DataFrame
data = [['New York Yankees', 'Acevedo Juan', 900000, 'Pitcher'],
['New York Yankees', 'Anderson Jason', 300000, 'Pitcher'],
['New York Yankees', 'Clemens Roger', 10100000, 'Pitcher'],
['New York Yankees', 'Contreras Jose', 5500000, 'Pitcher']]
DataFrame.from_records(data)





share|improve this answer











$endgroup$









  • 1




    $begingroup$
    You could refine it a bit more with: DataFrame.from_records(data, columns=['Team', 'Player', 'whatever-stat-is-that', 'position'])
    $endgroup$
    – Juan Ignacio Gil
    Jan 11 '18 at 10:14










  • $begingroup$
    Is there a way to specify the imports more specifically? E.g. I want to specify that DataFrame["Team"] must refer to the first item of each sublist (i.e. data[i][0]) and DataFrame["Position"] to refer to the last item of each sublist (i.e. data[i][-1])?
    $endgroup$
    – Ivo
    Jan 17 at 15:20










  • $begingroup$
    @Ivo: Use the columns parameter of DataFrame.from_records.
    $endgroup$
    – Emre
    Jan 17 at 21:27





















3












$begingroup$

You can just directly define it as a data frame as follows:



import pandas as pd

data = [['New York Yankees', 'Acevedo Juan', 900000, 'Pitcher'],
['New York Yankees', 'Anderson Jason', 300000, 'Pitcher'],
['New York Yankees', 'Clemens Roger', 10100000, 'Pitcher'],
['New York Yankees', 'Contreras Jose', 5500000, 'Pitcher']]

data = pd.DataFrame(data)





share|improve this answer









$endgroup$





















    3












    $begingroup$

    Once you have the data:



    import pandas as pd

    data = [['New York Yankees ', '"Acevedo Juan" ', 900000, ' Pitchern'],
    ['New York Yankees ', '"Anderson Jason"', 300000, ' Pitchern'],
    ['New York Yankees ', '"Clemens Roger" ', 10100000, ' Pitchern'],
    ['New York Yankees ', '"Contreras Jose"', 5500000, ' Pitchern']]


    You can create dataframe from the transposing the data:



    data_transposed = zip(data)
    df = pd.DataFrame(data_transposed, columns=["Team", "Player", "Salary", "Role"])


    Another way:



    df = pd.DataFrame(data)
    df = df.transpose()
    df.columns = ["Team", "Player", "Salary", "Role"]





    share|improve this answer











    $endgroup$













      Your Answer





      StackExchange.ifUsing("editor", function () {
      return StackExchange.using("mathjaxEditing", function () {
      StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
      StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
      });
      });
      }, "mathjax-editing");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "557"
      };
      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: false,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      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%2fdatascience.stackexchange.com%2fquestions%2f26333%2fconvert-a-list-of-lists-into-a-pandas-dataframe%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      13












      $begingroup$

      from pandas import DataFrame
      data = [['New York Yankees', 'Acevedo Juan', 900000, 'Pitcher'],
      ['New York Yankees', 'Anderson Jason', 300000, 'Pitcher'],
      ['New York Yankees', 'Clemens Roger', 10100000, 'Pitcher'],
      ['New York Yankees', 'Contreras Jose', 5500000, 'Pitcher']]
      DataFrame.from_records(data)





      share|improve this answer











      $endgroup$









      • 1




        $begingroup$
        You could refine it a bit more with: DataFrame.from_records(data, columns=['Team', 'Player', 'whatever-stat-is-that', 'position'])
        $endgroup$
        – Juan Ignacio Gil
        Jan 11 '18 at 10:14










      • $begingroup$
        Is there a way to specify the imports more specifically? E.g. I want to specify that DataFrame["Team"] must refer to the first item of each sublist (i.e. data[i][0]) and DataFrame["Position"] to refer to the last item of each sublist (i.e. data[i][-1])?
        $endgroup$
        – Ivo
        Jan 17 at 15:20










      • $begingroup$
        @Ivo: Use the columns parameter of DataFrame.from_records.
        $endgroup$
        – Emre
        Jan 17 at 21:27


















      13












      $begingroup$

      from pandas import DataFrame
      data = [['New York Yankees', 'Acevedo Juan', 900000, 'Pitcher'],
      ['New York Yankees', 'Anderson Jason', 300000, 'Pitcher'],
      ['New York Yankees', 'Clemens Roger', 10100000, 'Pitcher'],
      ['New York Yankees', 'Contreras Jose', 5500000, 'Pitcher']]
      DataFrame.from_records(data)





      share|improve this answer











      $endgroup$









      • 1




        $begingroup$
        You could refine it a bit more with: DataFrame.from_records(data, columns=['Team', 'Player', 'whatever-stat-is-that', 'position'])
        $endgroup$
        – Juan Ignacio Gil
        Jan 11 '18 at 10:14










      • $begingroup$
        Is there a way to specify the imports more specifically? E.g. I want to specify that DataFrame["Team"] must refer to the first item of each sublist (i.e. data[i][0]) and DataFrame["Position"] to refer to the last item of each sublist (i.e. data[i][-1])?
        $endgroup$
        – Ivo
        Jan 17 at 15:20










      • $begingroup$
        @Ivo: Use the columns parameter of DataFrame.from_records.
        $endgroup$
        – Emre
        Jan 17 at 21:27
















      13












      13








      13





      $begingroup$

      from pandas import DataFrame
      data = [['New York Yankees', 'Acevedo Juan', 900000, 'Pitcher'],
      ['New York Yankees', 'Anderson Jason', 300000, 'Pitcher'],
      ['New York Yankees', 'Clemens Roger', 10100000, 'Pitcher'],
      ['New York Yankees', 'Contreras Jose', 5500000, 'Pitcher']]
      DataFrame.from_records(data)





      share|improve this answer











      $endgroup$



      from pandas import DataFrame
      data = [['New York Yankees', 'Acevedo Juan', 900000, 'Pitcher'],
      ['New York Yankees', 'Anderson Jason', 300000, 'Pitcher'],
      ['New York Yankees', 'Clemens Roger', 10100000, 'Pitcher'],
      ['New York Yankees', 'Contreras Jose', 5500000, 'Pitcher']]
      DataFrame.from_records(data)






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Jan 6 '18 at 0:58

























      answered Jan 5 '18 at 18:46









      EmreEmre

      8,54011935




      8,54011935








      • 1




        $begingroup$
        You could refine it a bit more with: DataFrame.from_records(data, columns=['Team', 'Player', 'whatever-stat-is-that', 'position'])
        $endgroup$
        – Juan Ignacio Gil
        Jan 11 '18 at 10:14










      • $begingroup$
        Is there a way to specify the imports more specifically? E.g. I want to specify that DataFrame["Team"] must refer to the first item of each sublist (i.e. data[i][0]) and DataFrame["Position"] to refer to the last item of each sublist (i.e. data[i][-1])?
        $endgroup$
        – Ivo
        Jan 17 at 15:20










      • $begingroup$
        @Ivo: Use the columns parameter of DataFrame.from_records.
        $endgroup$
        – Emre
        Jan 17 at 21:27
















      • 1




        $begingroup$
        You could refine it a bit more with: DataFrame.from_records(data, columns=['Team', 'Player', 'whatever-stat-is-that', 'position'])
        $endgroup$
        – Juan Ignacio Gil
        Jan 11 '18 at 10:14










      • $begingroup$
        Is there a way to specify the imports more specifically? E.g. I want to specify that DataFrame["Team"] must refer to the first item of each sublist (i.e. data[i][0]) and DataFrame["Position"] to refer to the last item of each sublist (i.e. data[i][-1])?
        $endgroup$
        – Ivo
        Jan 17 at 15:20










      • $begingroup$
        @Ivo: Use the columns parameter of DataFrame.from_records.
        $endgroup$
        – Emre
        Jan 17 at 21:27










      1




      1




      $begingroup$
      You could refine it a bit more with: DataFrame.from_records(data, columns=['Team', 'Player', 'whatever-stat-is-that', 'position'])
      $endgroup$
      – Juan Ignacio Gil
      Jan 11 '18 at 10:14




      $begingroup$
      You could refine it a bit more with: DataFrame.from_records(data, columns=['Team', 'Player', 'whatever-stat-is-that', 'position'])
      $endgroup$
      – Juan Ignacio Gil
      Jan 11 '18 at 10:14












      $begingroup$
      Is there a way to specify the imports more specifically? E.g. I want to specify that DataFrame["Team"] must refer to the first item of each sublist (i.e. data[i][0]) and DataFrame["Position"] to refer to the last item of each sublist (i.e. data[i][-1])?
      $endgroup$
      – Ivo
      Jan 17 at 15:20




      $begingroup$
      Is there a way to specify the imports more specifically? E.g. I want to specify that DataFrame["Team"] must refer to the first item of each sublist (i.e. data[i][0]) and DataFrame["Position"] to refer to the last item of each sublist (i.e. data[i][-1])?
      $endgroup$
      – Ivo
      Jan 17 at 15:20












      $begingroup$
      @Ivo: Use the columns parameter of DataFrame.from_records.
      $endgroup$
      – Emre
      Jan 17 at 21:27






      $begingroup$
      @Ivo: Use the columns parameter of DataFrame.from_records.
      $endgroup$
      – Emre
      Jan 17 at 21:27













      3












      $begingroup$

      You can just directly define it as a data frame as follows:



      import pandas as pd

      data = [['New York Yankees', 'Acevedo Juan', 900000, 'Pitcher'],
      ['New York Yankees', 'Anderson Jason', 300000, 'Pitcher'],
      ['New York Yankees', 'Clemens Roger', 10100000, 'Pitcher'],
      ['New York Yankees', 'Contreras Jose', 5500000, 'Pitcher']]

      data = pd.DataFrame(data)





      share|improve this answer









      $endgroup$


















        3












        $begingroup$

        You can just directly define it as a data frame as follows:



        import pandas as pd

        data = [['New York Yankees', 'Acevedo Juan', 900000, 'Pitcher'],
        ['New York Yankees', 'Anderson Jason', 300000, 'Pitcher'],
        ['New York Yankees', 'Clemens Roger', 10100000, 'Pitcher'],
        ['New York Yankees', 'Contreras Jose', 5500000, 'Pitcher']]

        data = pd.DataFrame(data)





        share|improve this answer









        $endgroup$
















          3












          3








          3





          $begingroup$

          You can just directly define it as a data frame as follows:



          import pandas as pd

          data = [['New York Yankees', 'Acevedo Juan', 900000, 'Pitcher'],
          ['New York Yankees', 'Anderson Jason', 300000, 'Pitcher'],
          ['New York Yankees', 'Clemens Roger', 10100000, 'Pitcher'],
          ['New York Yankees', 'Contreras Jose', 5500000, 'Pitcher']]

          data = pd.DataFrame(data)





          share|improve this answer









          $endgroup$



          You can just directly define it as a data frame as follows:



          import pandas as pd

          data = [['New York Yankees', 'Acevedo Juan', 900000, 'Pitcher'],
          ['New York Yankees', 'Anderson Jason', 300000, 'Pitcher'],
          ['New York Yankees', 'Clemens Roger', 10100000, 'Pitcher'],
          ['New York Yankees', 'Contreras Jose', 5500000, 'Pitcher']]

          data = pd.DataFrame(data)






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 6 '18 at 5:43









          LUSAQXLUSAQX

          348416




          348416























              3












              $begingroup$

              Once you have the data:



              import pandas as pd

              data = [['New York Yankees ', '"Acevedo Juan" ', 900000, ' Pitchern'],
              ['New York Yankees ', '"Anderson Jason"', 300000, ' Pitchern'],
              ['New York Yankees ', '"Clemens Roger" ', 10100000, ' Pitchern'],
              ['New York Yankees ', '"Contreras Jose"', 5500000, ' Pitchern']]


              You can create dataframe from the transposing the data:



              data_transposed = zip(data)
              df = pd.DataFrame(data_transposed, columns=["Team", "Player", "Salary", "Role"])


              Another way:



              df = pd.DataFrame(data)
              df = df.transpose()
              df.columns = ["Team", "Player", "Salary", "Role"]





              share|improve this answer











              $endgroup$


















                3












                $begingroup$

                Once you have the data:



                import pandas as pd

                data = [['New York Yankees ', '"Acevedo Juan" ', 900000, ' Pitchern'],
                ['New York Yankees ', '"Anderson Jason"', 300000, ' Pitchern'],
                ['New York Yankees ', '"Clemens Roger" ', 10100000, ' Pitchern'],
                ['New York Yankees ', '"Contreras Jose"', 5500000, ' Pitchern']]


                You can create dataframe from the transposing the data:



                data_transposed = zip(data)
                df = pd.DataFrame(data_transposed, columns=["Team", "Player", "Salary", "Role"])


                Another way:



                df = pd.DataFrame(data)
                df = df.transpose()
                df.columns = ["Team", "Player", "Salary", "Role"]





                share|improve this answer











                $endgroup$
















                  3












                  3








                  3





                  $begingroup$

                  Once you have the data:



                  import pandas as pd

                  data = [['New York Yankees ', '"Acevedo Juan" ', 900000, ' Pitchern'],
                  ['New York Yankees ', '"Anderson Jason"', 300000, ' Pitchern'],
                  ['New York Yankees ', '"Clemens Roger" ', 10100000, ' Pitchern'],
                  ['New York Yankees ', '"Contreras Jose"', 5500000, ' Pitchern']]


                  You can create dataframe from the transposing the data:



                  data_transposed = zip(data)
                  df = pd.DataFrame(data_transposed, columns=["Team", "Player", "Salary", "Role"])


                  Another way:



                  df = pd.DataFrame(data)
                  df = df.transpose()
                  df.columns = ["Team", "Player", "Salary", "Role"]





                  share|improve this answer











                  $endgroup$



                  Once you have the data:



                  import pandas as pd

                  data = [['New York Yankees ', '"Acevedo Juan" ', 900000, ' Pitchern'],
                  ['New York Yankees ', '"Anderson Jason"', 300000, ' Pitchern'],
                  ['New York Yankees ', '"Clemens Roger" ', 10100000, ' Pitchern'],
                  ['New York Yankees ', '"Contreras Jose"', 5500000, ' Pitchern']]


                  You can create dataframe from the transposing the data:



                  data_transposed = zip(data)
                  df = pd.DataFrame(data_transposed, columns=["Team", "Player", "Salary", "Role"])


                  Another way:



                  df = pd.DataFrame(data)
                  df = df.transpose()
                  df.columns = ["Team", "Player", "Salary", "Role"]






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited 13 hours ago









                  Coolio2654

                  1255




                  1255










                  answered Oct 3 '18 at 10:27









                  Paloma ManzanoPaloma Manzano

                  311




                  311






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Data Science Stack Exchange!


                      • 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.


                      Use MathJax to format equations. MathJax reference.


                      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%2fdatascience.stackexchange.com%2fquestions%2f26333%2fconvert-a-list-of-lists-into-a-pandas-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 to label and detect the document text images

                      Vallis Paradisi

                      Tabula Rosettana