JIYIK CN >

Current Location:Home > Learning > PROGRAM > Python >

Convert List to Pandas DataFrame in Python

Author:JIYIK Last Updated:2025/05/09 Views:

This article will show you how to convert items in a list into a Pandas DataFrame.


Convert List to Pandas DataFrame in Python

DataFrame, in general, is a two-dimensional labeled data structure. Pandas is an open source Python package that is very useful for data science.

Here, we will first import the pandas package. We will define the pandas package in this particular program as pd. We will then create a list my_listto store the list values Tom​​, Mark, and Tony, which are just random names. We will then pd.DataFrame(my_list)assign to the variable df. DataFrame(my_list)The method takes my_listthe value of and creates a DataFrame with it. In the last line of the program, we called dfprint DataFrame stored in the variable . Note that we could have also just written dfinstead of print(df)to see our DataFrame.

Sample code:

# python 3.x
import pandas as pd

my_list = ["Tom", "Mark", "Tony"]
df = pd.DataFrame(my_list)
print(df)

Output:

    0
0   Tom
1   Mark
2   Tony

We can see that the elements we provided in the list are now in one column of the above output.


Storing Lists in Pandas DataFrame Columns in Python

We can convert the list to a pandas DataFrame by creating a column in the DataFrame and storing the converted data in that column.

To convert a list into data for a Pandas DataFrame column, we will create a list my_listand give the list some random names as values. Our goal is to ensure that the list elements become Namesentries of a column titled . To do this, we will use pass columns = ['Names']the variable my_listto pd.DataFrame(), as shown below. We then print dfthe variable and run our code to see the output.

Sample code:

# python 3.x
import pandas as pd

my_list = ["Tom", "Mark", "Tony"]
df = pd.DataFrame(my_list, columns=["Names"])
print(df)

Output:

    Names
0   Tom
1   Mark
2   Tony

After we add an extra attribute columns = ['Names'], we see my_listthe names in as Namesthe values ​​of the columns in the DataFrame .


Convert a list to an indexed DataFrame in Python

We can also index the list items while converting them to a DataFrame.

We will create a list my_list. Our goal is to ensure that the list elements become column entries with predefined row indices titled 名称. To do this, we will create a list indexand fill it with i, ii, and iii. We can use a list as pd.DataFrame()the second argument in . The first and third arguments are my_listand columns =['Names']. We will then print the variables that store the expressions we wrote df.

Sample code:

# python 3.x
import pandas as pd

my_list = [" Tom", "Mark", "Tony"]
df = pd.DataFrame(my_list, index=["i.", "ii.", "iii."], columns=["Names"])
print(df)

Output:

     Names
i.    Tom
ii.   Mark
iii.  Tony

We can see that indexthe values ​​in the list have replaced the default Pandas index. We can put any value into indexand generate the result accordingly.


zip()Zip two lists into a single DataFrame in Python using

zip()The function merges the values ​​of two different lists into one by combining the values ​​of the lists with the same index. Before we create a DataFrame, let's see zip()how it works.

Sample code:

# python 3.x
a = ["1", "2", "3"]
b = ["4", "5", "6"]
c = zip(a, b)
list1 = list(c)
print(list1)

Output:

[('1', '4'), ('2', '5'), ('3', '6')]

We can see zip()that the function helps us combine the lists aand bwith grouped similar index items. We store the compressed state of the lists aand into and then create and store the compressed list into it. In the following example, we will create a Pandas DataFrame using .bclist1czip()

We will create two different lists, name_listand height_list, and store some names and heights respectively. Then we use zip(name_list, height_list)zip name_listand height_listto create a Pandas DataFrame.

Note that we can also index our data by simply putting another attribute index = [ 'index1', 'index2', 'index3' ], where the items in the index list can be anything.

Sample code:

# python 3.x
import pandas as pd

name_list = ["Tom", "Mark", "Tony"]
height_list = ["150", "151", "152"]
df = pd.DataFrame((zip(name_list, height_list)), columns=["Name", "Height"])
print(df)

Output:

    Name    Height
0   Tom     150
1   Mark    151
2   Tony    152

We can see that the resulting DataFrame consists of the values ​​of name_listand height_listin the correct order.

We can also use this technique to zip more than two lists.


Convert a Multidimensional List to a Pandas DataFrame in Python

We can even convert a multidimensional list to a Pandas DataFrame. We can set column names for the list items in a multidimensional list. We will demonstrate this method with a two-dimensional list.

To convert a multidimensional list to a Pandas DataFrame, we need to first create a list of lists. Therefore, we will first import Pandas and then create a list where infowe will store the names and ages of three different individuals in three separate lists. We will then call pd.DataFrame()and process the lists into it and specify two column headers for our data, , Nameand Age.

Sample code:

# python 3.x
import pandas as pd

info = [["Tom", 18], ["Mark", 25], ["Tony", 68]]
df = pd.DataFrame(info, columns=["Name", "Age"])
print(df)

Output:

    Name    Age
0   Tom     18
1   Mark    25
2   Tony    68

We have two columns as output, name and age in their respective order. We can infoadd additional values ​​to the individual lists in and assign column headers to them to get more columns in our DataFrame.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Finding a string in a list in Python

Publish Date:2025/05/09 Views:75 Category:Python

This tutorial shows you how to find elements from a Python list that have a specific substring in them. We will use the following list and extract ack the strings that have in it. my_list = [ "Jack" , "Mack" , "Jay" , "Mark" ] for Find elem

Getting list shape in Python

Publish Date:2025/05/09 Views:139 Category:Python

In Python, knowing the shape of a list is very important for working with data structures, especially when it comes to multidimensional or nested lists. This article explores various ways to determine the shape of a list in Python, from sim

Adding multiple elements to a list in Python

Publish Date:2025/05/09 Views:180 Category:Python

List is a mutable data structure in Python. It can contain values ​​of different types. This article will discuss some methods to append single or multiple elements to a Python list. append() Append a single element in a Python list usi

Get the index of the maximum and minimum values in a list in Python

Publish Date:2025/05/09 Views:117 Category:Python

In this tutorial, we will discuss methods to get the index of the maximum and minimum value of a list in Python. max() Get the index of the maximum value in a list using and list.index() functions in Python max() The function gives the maxi

List of numbers from 1 to N in Python

Publish Date:2025/05/09 Views:116 Category:Python

This tutorial will discuss how to create a list of numbers from 1 to some specified number. Create a user-defined function to create a list of numbers from 1 to N This method will take the desired number from the user and for iterate until

How to Find the Maximum Value in a List in Python

Publish Date:2025/05/09 Views:99 Category:Python

This tutorial will demonstrate how to find the maximum value in a list in Python. This tutorial will cover a number of scenarios and data types, from simple lists of integers to more complex structures like arrays within arrays. for Finding

Convert a list to a set in Python

Publish Date:2025/05/08 Views:145 Category:Python

This tutorial demonstrates how to convert a list to a set in Python. Difference between List and Set Lists and sets are standard Python data types that store values ​​in sequence. Python list stores comma separated values ​​between

Remove the first element from a list in Python

Publish Date:2025/05/08 Views:173 Category:Python

This tutorial will discuss different ways on how to remove the first element from a list. pop() Remove the first element from a list using the pop() Method can remove an element from a specific index. We have to specify the index from which

Convert a list to lowercase in Python

Publish Date:2025/05/08 Views:112 Category:Python

Lists can be used to store multiple items in a single variable. In Python, we can create a list of strings by enclosing the different elements in the list in single or double quotes. This tutorial demonstrates how to convert a list of strin

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial