Series.tolist() – Convert Pandas Series to List

  • Post author:
  • Post category:Pandas / Python
  • Post last modified:January 17, 2023
Spread the love

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.

1. Quick Examples to Convert Series to list

If you are in hurry below are some quick examples of how to convert series to list.


# Below are some quick examples.

#  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'])

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

3. 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 (s)

Yields below output.


Courses     pandas
Fees         20000
Duration    30days
dtype: object

4. 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']

5. 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']

6. Use Type Casting Method 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']

Conclusion

In this article, I have explained 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.

References

Leave a Reply

You are currently viewing Series.tolist() – Convert Pandas Series to List