JIYIK CN >

Current Location:Home > Learning > PROGRAM > Python >

Convert Pandas DataFrame columns to lists

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

When working with Pandas DataFrames in Python, you often need to convert the columns of the DataFrame into Python lists. This process is very important for various data manipulation and analysis tasks. Fortunately, Pandas provides several methods to do this, making it easy to extract the required data in list form.

In this article, we will explore different ways to convert Pandas DataFrame columns to Python lists. We will use a sample DataFrame containing name, date of birth (DOB), and salary information to demonstrate these methods.

import pandas as pd

data = {
    "Name": ["James", "Michelina", "Marc", "Bob", "Halena"],
    "DOB": ["1/1/2014", "2/1/2014", "3/1/2014", "4/1/2014", "4/1/2014"],
    "Salary": ["1000", "12000", "36000", "15000", "12000"],
}

df = pd.DataFrame(data, columns=["Name", "DOB", "Salary"])

We’ll explore four different ways to perform this conversion: using tolist()methods, list()functions, .valuesproperties, and list comprehensions. Each approach has its advantages, so let’s take a closer look.


tolist()Convert DataFrame columns to lists using

The columns in Pandas dataframe are Pandas . SeriesSo if we need to convert a column into a list, we can Seriesuse tolist() method in .tolist()Series

In the following code, the columns named or df['DOB']in the DataFrame are returned .DOBSeries

tolist()method converts a Series to a list.

import pandas as pd

df = pd.DataFrame(
    [
        ["James", "1/1/2014", "1000"],
        ["Michelina", "2/1/2014", "12000"],
        ["Marc", "3/1/2014", "36000"],
        ["Bob", "4/1/2014", "15000"],
        ["Halena", "4/1/2014", "12000"],
    ],
    columns=["Name", "DOB", "Salary"],
)

print("Pandas DataFrame:\n\n", df, "\n")

list_of_single_column = df["DOB"].tolist()

print(
    "the list of a single column from the dataframe\n",
    list_of_single_column,
    "\n",
    type(list_of_single_column),
)

Output:

Pandas DataFrame:

         Name       DOB Salary
0      James  1/1/2014   1000
1  Michelina  2/1/2014  12000
2       Marc  3/1/2014  36000
3        Bob  4/1/2014  15000
4     Halena  4/1/2014  12000 

the list of a single column from the dataframe
 ['1/1/2014', '2/1/2014', '3/1/2014', '4/1/2014', '4/1/2014'] 
 <class 'list'>

list()Convert DataFrame column to list using function

We can also use list()a function to convert a DataFrame column into a list, by passing the DataFrame to list()the function.

We will use the same data above to demonstrate this approach.

import pandas as pd

df = pd.DataFrame(
    [
        ["James", "1/1/2014", "1000"],
        ["Michelina", "2/1/2014", "12000"],
        ["Marc", "3/1/2014", "36000"],
        ["Bob", "4/1/2014", "15000"],
        ["Halena", "4/1/2014", "12000"],
    ],
    columns=["Name", "DOB", "Salary"],
)

print("Pandas DataFrame:\n\n", df, "\n")

list_of_single_column = list(df["DOB"])

print(
    "the list of a single column from the dataframe\n",
    list_of_single_column,
    "\n",
    type(list_of_single_column),
)

Output:

Pandas DataFrame:

         Name       DOB Salary
0      James  1/1/2014   1000
1  Michelina  2/1/2014  12000
2       Marc  3/1/2014  36000
3        Bob  4/1/2014  15000
4     Halena  4/1/2014  12000 

the list of a single column from the dataframe
 ['1/1/2014', '2/1/2014', '3/1/2014', '4/1/2014', '4/1/2014'] 
 <class 'list'>

.valuesConvert DataFrame column to list using attributes

Another way to achieve this is to use .valuesproperties.

.valuesLet’s demonstrate by going through the steps of converting the ‘Salary’ column of this DataFrame into a Python list using attributes.

The attributes of a Pandas Series .valuesreturn a NumPy array representation of the data. To convert it to a Python list, you can use .tolist()the method. Here is how to do it:

salary_list = df["Salary"].values.tolist()

Here is the complete code:

import pandas as pd

data = {
    "Name": ["James", "Michelina", "Marc", "Bob", "Halena"],
    "DOB": ["1/1/2014", "2/1/2014", "3/1/2014", "4/1/2014", "4/1/2014"],
    "Salary": ["1000", "12000", "36000", "15000", "12000"],
}

df = pd.DataFrame(data, columns=["Name", "DOB", "Salary"])

# Convert the 'Salary' column to a list using .values
salary_list = df["Salary"].values.tolist()

print(salary_list)

Output:

['1000', '12000', '36000', '15000', '12000']

After executing this code, the "salary" column in the DataFrame will be converted into a salary_listPython list stored in the variable.


Convert DataFrame columns to lists using list comprehensions

List comprehensions are a concise and efficient way to create lists in Python. To convert a column of a Pandas DataFrame into a Python list using list comprehensions, you can follow the code below.

# Using list comprehension to convert the 'Salary' column to a list
salary_list = [salary for salary in df["Salary"]]

Here is the complete code:

import pandas as pd

data = {
    "Name": ["James", "Michelina", "Marc", "Bob", "Halena"],
    "DOB": ["1/1/2014", "2/1/2014", "3/1/2014", "4/1/2014", "4/1/2014"],
    "Salary": [1000, 12000, 36000, 15000, 12000],
}

df = pd.DataFrame(data, columns=["Name", "DOB", "Salary"])

# Using list comprehension to convert the 'Salary' column to a list
salary_list = [salary for salary in df["Salary"]]

print(salary_list)

Output:

[1000, 12000, 36000, 15000, 12000]

in conclusion

In this article, we explored four different ways to convert a Pandas DataFrame column to a Python list: using tolist()methods, list()functions, .valuesattributes, and list comprehensions. Each method has its advantages, and you can choose which one to use based on your specific use case and coding style.

Whether you prefer tolist()the simplicity of the method, list()the standard Python style of the function, .valuesthe efficiency of the attribute, or the readability of list comprehensions, Pandas provides a variety of options to help you easily convert DataFrame columns to Python lists.

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

Subtracting Two Columns in Pandas DataFrame

Publish Date:2025/05/01 Views:120 Category:Python

Pandas can handle very large data sets and has a variety of functions and operations that can be applied to the data. One of the simple operations is to subtract two columns and store the result in a new column, which we will discuss in thi

Dropping columns by index in Pandas DataFrame

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

DataFrames can be very large and can contain hundreds of rows and columns. It is necessary to master the basic maintenance operations of DataFrames, such as deleting multiple columns. We can use dataframe.drop() the method to delete columns

Pandas Copy DataFrame

Publish Date:2025/05/01 Views:53 Category:Python

This tutorial will show you how to DataFrame.copy() copy a DataFrame object using the copy method. import pandas as pd items_df = pd . DataFrame( { "Id" : [ 302 , 504 , 708 ], "Cost" : [ "300" , "400" , "350" ], } ) print (items_df) Output:

Pandas DataFrame.ix[] Function

Publish Date:2025/05/01 Views:168 Category:Python

Python Pandas DataFrame.ix[] function slices rows or columns based on the value of the argument. pandas.DataFrame.ix[] grammar DataFrame . ix[index = None , label = None ] parameter index Integer or list of integers used to slice row indice

Pandas DataFrame.describe() Function

Publish Date:2025/05/01 Views:120 Category:Python

Python Pandas DataFrame.describe() function returns the statistics of a DataFrame. pandas.DataFrame.describe() grammar DataFrame . describe( percentiles = None , include = None , exclude = None , datetime_is_numeric = False ) parameter perc

Pandas DataFrame.astype() Function

Publish Date:2025/05/01 Views:160 Category:Python

Python Pandas DataFrame.astype() function changes the data type of an object to the specified data type. pandas.DataFrame.astype() grammar DataFrame . astype(dtype, copy = True , errors = "raise" ) parameter dtype The data type we want to a

Pandas DataFrame.to_dict() Function

Publish Date:2025/05/01 Views:188 Category:Python

Python Pandas DataFrame.to_dict() function converts the given DataFrame to a dictionary. pandas.DataFrame.to_dict() Syntax DataFrame . to_dict(orient = 'dict' , into = class ' dict ' ) parameter orient This parameter determines the type of

Pandas DataFrame.reset_index() Function

Publish Date:2025/05/01 Views:140 Category:Python

Python Pandas DataFrame.reset_index() function resets the index of the given DataFrame. It replaces the old index with the default index. If the given DataFrame has a MultiIndex, then this method removes all the levels. pandas.DataFrame.rep

Pandas DataFrame.resample() Function

Publish Date:2025/05/01 Views:78 Category:Python

Python Pandas DataFrame.resample() function resamples time series data. pandas.DataFrame.resample() Syntax DataFrame . resample( rule, axis = 0 , closed = None , label = None , convention = "start" , kind = None , loffset = None , base = No

Scan to Read All Tech Tutorials

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

Recommended

Tags

Scan the Code
Easier Access Tutorial