Facebook Twitter Instagram
    Facebook Twitter Instagram Pinterest Vimeo
    Hand On CodeHand On Code
    Hand On CodeHand On Code
    Home»python»Convert List to dataframe in Python
    python

    Convert List to dataframe in Python

    April 2, 2023No Comments5 Mins Read
    Share
    Facebook Twitter LinkedIn Pinterest Email

    Python List Dataframe Conversion

    In this guide, we’ll learn how to take use of Python’s list comprehension and transform it into a dataframe.

    But before we get started, let’s go through what the list is and what dataframes are.

    In Python, a list is a data structure whose items are denoted by square brackets.

    A list as an illustration

    is-



    1. Colors=[


      ‘Red’


      ,


      ‘Blue’


      ,


      ‘Green’


      ,


      ‘Orange’


      ]

    Data frames are the rows and columns that make up a data table.

    Importing pandas allows for their utilization.

    Let’s have a look at the many ways we may transform that list into a dataframe in

    Python.

    1. Using

      DataFrame()
    2. Using list with index and column names
    3. Using zip()
    4. Using Multidimensional list
    5. Using multidimensional list with column and data type
    6. Using lists in the dictionary

    Using pd.DataFrame()

    In the first method, we have utilized the pd.DataFrame() function to transform a list into a data frame.

    The following code demonstrates how it is

    done-

    Product Code:



    1. import


      pandas as pd


    2. #list_values having strings

    3. list_values = [

      ‘English’


      ,


      ‘Hindi’


      ,


      ‘Mathematics’


      ,


      ‘Science’


      ,


      ‘Social Science’


      ]


    4. df = pd.DataFrame(list_values)

    5. print(df)
                    0
    0         English
    1           Hindi
    2     Mathematics
    3         Science
    4  Social Science
    

    Clarification:

    Now let’s have a look at why the aforementioned happened.

    program-

    1. In the first step we have imported the pandas library.
    2. After this, we have declared the list that has strings as its values.
    3. Finally, we have passed this list in

      DataFrame()

      and displayed the output.

    Using List with Index and Column Names

    Method two involves the construction of a dataframe with a specified index value and a named column.

    The code sample provided here demonstrates

    same.

    Resulting



    1. import


      pandas as pd


    2. #list_values having strings

    3. list_values = [

      ‘English’


      ,


      ‘Hindi’


      ,


      ‘Mathematics’


      ,


      ‘Science’


      ,


      ‘Social Science’


      ]


    4. df = pd.DataFrame(list_values,index = [

      ‘i’


      ,


      ‘ii’


      ,


      ‘iii’


      ,


      ‘iv’


      ,


      ‘v’


      ], columns = [


      ‘Subjects’


      ])


    5. print(df)

    :

    Explanation:

               Subjects
    i           English
    ii            Hindi
    iii     Mathematics
    iv          Science
    v    Social Science
    

    Let’s break down the aforementioned, shall we?

    program-

    1. In the first step we have imported the pandas library.
    2. After this, we have declared the list that has strings as it’s values.
    3. Finally, we have passed this list in

      DataFrame()

      with a list of index values and the column name.
    4. On executing the program, it displays the desired output.

    Using zip()

    Here, we make use of the zip format ().

    The following code demonstrates how it is

    done-

    Resulting



    1. import


      pandas as pd


    2. #list_values having strings

    3. list_values = [

      ‘English’


      ,


      ‘Hindi’


      ,


      ‘Mathematics’


      ,


      ‘Science’


      ,


      ‘Social Science’


      ]


    4. list_index = [

      20


      ,


      21


      ,


      22


      ,


      23


      ,


      24


      ]


    5. df = pd.DataFrame(list(zip(list_values, list_index)), columns = [

      ‘Subjects’


      ,


      ‘Code’


      ])


    6. print(df)

    Explanation:

             Subjects    Code
    0         English       20
    1           Hindi          21
    2     Mathematics    22
    3         Science          23
    4  Social Science      24
    

    Now let’s have a look at why the aforementioned happened.

    program-

    1. In the first step, we have imported the pandas library.
    2. After this, we have declared the list that has strings as its values and another list contains the index values.
    3. Finally, we have passed the

      list_values

      and

      list_index

      in zip inside

      DataFrame()

      with a list of index values and the column name.
    4. On executing the program, it displays the desired output.

    Using Multidimensional List

    We’ll examine the usage of a multidimensional list in a transformational context.

    The code provided here is an example of

    same.

    Resulting



    1. import


      pandas as pd


    2. #list_values having strings

    3. list_values = [[

      ‘English’


      ,


      4101


      ], [


      ‘Hindi’


      ,


      4102


      ], [


      ‘Science’


      ,


      4103


      ], [


      ‘Mathematics’


      ,


      4104


      ], [


      ‘Computer’


      ,


      4105


      ]]


    4. df = pd.DataFrame(list_values, columns = [

      ‘Subject Name’


      ,


      ‘Subject Code’


      ])


    5. print(df)

    :

    Subject Name  Subject Code
    0      English          4101
    1        Hindi          4102
    2      Science          4103
    3  Mathematics          4104
    4     Computer          4105
    

    Clarification:

    The moment has come to comprehend the aforementioned.

    program-

    1. In the first step we have imported the pandas library.
    2. After this, we have declared the list contains different lists and each list has a string and an integer value.
    3. Finally, we have passed the list_values in pd.DataFrame() with a list of column names.
    4. On executing the program, it displays the desired output.

    Using Multidimensional List with Column and Data Type

    In this method, the preceding protocol will be slightly modified.

    The following code demonstrates how it is

    done-

    Resulting



    1. import


      pandas as pd


    2. #list_values having strings

    3. list_values = [[

      ‘Colin’


      ,


      ‘Lassiter’


      ,


      46


      ], [


      ‘James’


      ,


      ‘Gomez’


      ,


      24


      ], [


      ‘Sara’


      ,


      ‘Charles’


      ,


      34


      ], [


      ‘Raven’


      ,


      ‘Stewart’


      ,


      24


      ], [


      ‘Oliver’


      ,


      ‘Osment’


      ,


      21


      ]]


    4. df = pd.DataFrame(list_values, columns = [

      ‘First_Name’


      ,


      ‘Last_Name’


      ,


      ‘Age’


      ], dtype =


      float


      )


    5. print(df)

    Explanation:

             First_Name  Last_Name   Age
    0      Colin                  Lassiter      46.0
    1      James                Gomez       24.0
    2       Sara                  Charles      34.0
    3      Raven                Stewart      24.0
    4     Oliver                 Osment      21.0
    

    Now let’s have a look at why the aforementioned happened.

    program-

    1. In the first step, we have imported the pandas library.
    2. After this, we have declared the list contains different lists and each list has two string

      values(first name and last name)

      and an integer

      value(age).
    3. Finally, we have passed the

      list_values

      in

      DataFrame()

      with a list of column names and the data type.
    4. On executing the program, it displays the desired output.

    Using Lists in the Dictionary

    In the final approach, we’ll merge lists and dictionaries by turning the list into a dataframe.

    The code provided here is an example of

    same.

    Resulting



    1. import


      pandas as pd


    2. #list_values having strings

    3. f_name = [

      ‘Colin’


      ,


      ‘James’


      ,


      ‘Sara’


      ,


      ‘Raven’


      ,


      ‘Oliver’


      ]


    4. l_name = [

      ‘Lassiter’


      ,


      ‘Gomez’


      ,


      ‘Charles’


      ,


      ‘Stewart’


      ,


      ‘Osment’


      ]


    5. age = [

      46


      ,


      24


      ,


      34


      ,


      24


      ,


      21


      ]


    6. dict = {

      ‘First Name’


      :f_name,


      ‘Last_Name’


      :l_name,


      ‘Age’


      :age}


    7. df = pd.DataFrame(dict)

    8. print(df)

    :

    Explanation:

           First Name   Last_Name    Age
    0      Colin               Lassiter         46
    1      James            Gomez           24
    2       Sara              Charles           34
    3      Raven            Stewart          24
    4     Oliver             Osment          21
    

    The moment has come to comprehend the aforementioned.

    program-

    1. In the first step, we have imported the pandas library.
    2. After this, we have declared three lists, namely f_name, l_name, and age.
    3. In the next step, we have used these lists as values for the keys of the dictionary.
    4. Finally, we have passed dict in

      DataFrame().
    5. On executing the program, it displays the desired output.
    Convert List to dataframe in Python Learn Python free Python Code Python Course Free download python coursefree Courses Download Python Language
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Previous ArticleDjango Migrations 101
    Next Article Python News Whats New From July 2022

    Related Posts

    python

    Class method vs Static method in Python

    April 7, 2023
    python

    Python Program to Count the Number of Matching Characters in a Pair of String

    April 7, 2023
    python

    Coroutine in Python

    April 7, 2023
    Add A Comment

    Leave A Reply Cancel Reply

    Facebook Twitter Instagram Pinterest
    © 2023 ThemeSphere. Designed by ThemeSphere.

    Type above and press Enter to search. Press Esc to cancel.