Pandas Series.tolist()
method is used to convert a Series to a list in Python. In case you need a Series object as a return type use series()
function to easily convert the list, tuple, and dictionary into a Series. In this article, we can see how to convert the pandas series to a list, and also we can see how to convert the Pandas DataFrame column to a list with several examples.
Key Points –
Series.tolist()
is a convenient method to convert a Pandas Series into a regular Python list.- The method converts all elements in the Series into a list while maintaining their original data types.
- The order of elements in the Series is preserved when converting to a list.
tolist()
is efficient for smaller datasets but may be slower for very large Series.tolist()
can be applied to individual DataFrame columns to convert them into lists.- The index of the Series is not included in the resulting list; only the values are returned.
tolist()
does not modify the original Series; it returns a new list.
Quick Examples to Convert Series to List
If you are in hurry below are some quick examples of how to convert series to list.
# Quick examples to convert series to list
# Example 1: Convert pandas Series to List
data = {'Courses' :"pandas", 'Fees' : 20000, 'Duration' : "30days"}
s = pd.Series(data)
listObj = s.tolist()
# Example 2: Convert the Course column of the DataFrame
# To a list
listObj = df['Courses'].tolist()
# Example 3: Convert a pandas series
# To a list using type casting
listObj = list(s)
# Example 4: Convert a DataFrame column
# To a list using type casting
listObj = list(df['Courses'])
Syntax of Pandas.Series.tolist()
Following is the syntax of the Pandas.Series.tolist()
.
# Syntax of Series.tolist()
Pandas.Series.tolist()
It returns the list of values.
Create Series From Dictionary
Pandas Series is a one-dimensional array that is capable of storing various data types (integer, string, float, python objects, etc.). In pandas Series, the row labels of the Series are called the index. The Series can have only one column. A List, NumPy Array, Dict can be turned into a Series.
The following example creates a Series from a Python dictionary using pd.Series()
function.
# Create a Dict from a input
import pandas as pd
data = {'Courses':"pandas", 'Fees':20000, 'Duration':"30days"}
s = pd.Series(data)
print("Pandas Series:\n",s)
Yields below output.
Usage of Pandas Series tolist()
In Python, pandas is the most efficient library for providing various functions to convert one data structure to another data structure. Series.tolist()
is one of the functions to convert the structure of the data. Using this function we are able to convert Series to Python list easily. Let’s take an example.
# Create a list from Series
listObj = s.tolist()
print("Our list:", listObj)
# Output :
# Our list: ['pandas', 20000, '30days']
Convert DataFrame Column (Series) to List
We consider that the columns of a pandas DataFrame are pandas Series objects hence, we can convert the columns of DataFrame into a list using the tolist()
method. First, let’s create Pandas DataFrame from dictionary using panads.DataFrame()
function and then use tolist()
to convert one of the column (series) to list. For example,
# Create Dict object
courses = {'Courses':['Spark','PySpark','Java','pandas'],
'Fee':[20000,20000,15000,20000],
'Duration':['35days','35days','40days','30days']}
# Create DataFrame from dict
df = pd.DataFrame.from_dict(courses)
print(df)
Yields below output.
# Output:
Courses Fee Duration
0 Spark 20000 35days
1 PySpark 20000 35days
2 Java 15000 40days
3 pandas 20000 30days
After creating DataFrame we have to pass one of its columns which, we want to convert to a list into this function, it returns the series as a list.
# Convert the Course column of the DataFrame to a list
listObj = df['Courses'].tolist()
print("Our list:", listObj)
print(type(listObj))
Yields below output.
# Output:
Our list: ['Spark', 'PySpark', 'Java', 'PHP']
Use Type Casting Method to Convert Series to List
Type casting is the process to convert one datatype to another datatype. Using type casting we can convert a series to a list in pandas directly. For that, we need to pass the series into the list()
function.
# Convert a pandas series to a list using type casting
listObj = list(s)
print('Our list:', listObj)
# Output :
# Our list: ['pandas', 20000, '30days']
We can also perform type casting to convert a DataFrame column to a list. For example.
# Convert a DataFrame column to a list using type casting
listObj = list(df['Courses'])
print('Our list:', listObj)
# Output :
# Our list: ['Spark', 'PySpark', 'Java', 'PHP']
Frequently Asked Questions on Series to List
To convert a Pandas Series to a Python list, you can use either the tolist()
method of the Pandas Series or the built-in list()
function.
Besides tolist()
, you can also use the built-in list()
function to convert a Pandas Series to a Python list.
Both methods (tolist()
and list()
) achieve the same result, converting a Pandas Series to a Python list. However, the tolist()
method is a Pandas-specific method, while the list()
function is a built-in Python function. In most cases, both methods are interchangeable, and you can choose the one that you find more convenient or readable.
You can convert a specific column of a Pandas DataFrame to a Python list. You can achieve this by accessing the column using its name and then using the tolist()
method or the list()
function.
If your Pandas Series contains missing values (NaN), the resulting list will also contain them. The NaN values will be represented as None
in the list.
The order of elements in the Pandas Series will be preserved when converting it to a list. Both the tolist()
method and the list()
constructor maintain the order of elements.
Conclusion
In this article, I have explained how to convert pandas Series into a Python list by using Series.tolist()
method and also explained converting data columns to list and also using type casting.
Related Articles
- How to Sort pandas Series
- Pandas Series.mean() Function
- Convert Pandas Series to String
- Pandas Get Floor or Ceil of Series
- How To Get Value From Pandas Series?
- How to create Pandas Series in Python?
- Find Intersection Between Two Series in Pandas?
- Drop Rows From Pandas DataFrame Examples
- Change the Order of Pandas DataFrame Columns
- Pandas groupby() and sum() With Examples
- Difference Between loc and iloc in Pandas DataFrame
- Drop Single & Multiple Columns From Pandas DataFrame