Series to list Python

You can create Pandas Series from a list using this syntax:

pd.Series(list_name)

In the next section, you’ll see the steps to apply the above syntax using a simple example.

Steps to Create Pandas Series from a List

Step 1: Create a List

To start, let’s create a list that contains 5 names:

people_list = ['Jon','Mark','Maria','Jill','Jack'] print(people_list)

This is how the list would look like:

['Jon', 'Mark', 'Maria', 'Jill', 'Jack']

The ultimate goal is to create a Pandas Series from the above list.

Step 2: Create the Pandas Series

Next, create the Pandas Series using this template:

pd.Series(list_name)

For our example, the list_name is “people_list.” Therefore, the complete code to create the Pandas Series is:

import pandas as pd people_list = ['Jon','Mark','Maria','Jill','Jack'] my_series = pd.Series(people_list) print(my_series)

Once you run the code in Python, you’ll get the following Series:

0 Jon 1 Mark 2 Maria 3 Jill 4 Jack

Step 3 (optional): Verify that you Created the Series

You can quickly verify that you successfully created the Pandas Series by adding “print(type(my_series))” at the bottom of the code:

import pandas as pd people_list = ['Jon','Mark','Maria','Jill','Jack'] my_series = pd.Series(people_list) print(my_series) print(type(my_series))

Run the code, and you’ll be able to confirm that you got the Pandas Series:

0 Jon 1 Mark 2 Maria 3 Jill 4 Jack dtype: object <class 'pandas.core.series.Series'>

Change the Index of the Pandas Series

You may have noticed that each row is represented by a number (also known as the index) starting from 0:

0 Jon 1 Mark 2 Maria 3 Jill 4 Jack

Alternatively, you may assign another value/name to represent each row. For example, in the code below, the index=[‘A’,’B’,’C’,’D’,’E’] was added:

import pandas as pd people_list = ['Jon','Mark','Maria','Jill','Jack'] my_series = pd.Series(people_list, index=['A','B','C','D','E']) print(my_series)

You’ll now see the newly assigned values:

A Jon B Mark C Maria D Jill E Jack

Additional Resources

So far, you have seen how to create Pandas Series. You may also want to check the following guide to learn how to create Pandas DataFrame.

Finally, you can learn more about Pandas Series by visiting the Pandas Documentation.

At times, you may need to convert Pandas DataFrame into a list in Python.

But how would you do that?

To accomplish this task, you can use tolist as follows:

df.values.tolist()

In this short guide, you’ll see an example of using tolist to convert Pandas DataFrame into a list.

Example of using tolist to Convert Pandas DataFrame into a List

Let’s say that you have the following data about products and prices:

product price
Tablet 250
Printer 100
Laptop 1200
Monitor 300

You then decided to capture that data in Python using Pandas DataFrame.

At a certain point, you realize that you’d like to convert that Pandas DataFrame into a list.

To accomplish this goal, you may use the following Python code in order to convert the DataFrame into a list, where:

  • The top part of the code, contains the syntax to create the DataFrame with our data about products and prices
  • The bottom part of the code converts the DataFrame into a list using: df.values.tolist()

Here is the full Python code:

import pandas as pd data = {'product': ['Tablet','Printer','Laptop','Monitor'], 'price': [250,100,1200,300] } df = pd.DataFrame(data) products_list = df.values.tolist() print(products_list)

And once you run the code, you’ll get the following multi-dimensional list (i.e., list of lists):

[['Tablet', 250], ['Printer', 100], ['Laptop', 1200], ['Monitor', 300]]

Optionally, you may further confirm that you got a list by adding print(type(products_list)) at the bottom of the code:

import pandas as pd data = {'product': ['Tablet','Printer','Laptop','Monitor'], 'price': [250,100,1200,300] } df = pd.DataFrame(data) products_list = df.values.tolist() print(products_list) print(type(products_list))

As you can see, the original DataFrame was indeed converted into a list (as highlighted in yellow):

[['Tablet', 250], ['Printer', 100], ['Laptop', 1200], ['Monitor', 300]] <class 'list'>

Convert an Individual Column in the DataFrame into a List

Let’s say that you’d like to convert the ‘product‘ column into a list.

You can then use the following template in order to convert an individual column in the DataFrame into a list:

df['column_name'].values.tolist()

Here is the complete Python code to convert the ‘product’ column into a list:

import pandas as pd data = {'product': ['Tablet','Printer','Laptop','Monitor'], 'price': [250,100,1200,300] } df = pd.DataFrame(data) product = df['product'].values.tolist() print(product)

Run the code, and you’ll get the following list:

['Tablet', 'Printer', 'Laptop', 'Monitor']

What if you want to append an additional item (e.g., Keyboard) into the ‘product’ list?

In that case, simply add the following syntax:

product.append('Keyboard')

So the complete Python code would look like this:

import pandas as pd data = {'product': ['Tablet','Printer','Laptop','Monitor'], 'price': [250,100,1200,300] } df = pd.DataFrame(data) product = df['product'].values.tolist() product.append('Keyboard') print(product)

You’ll now see the ‘Keyboard’ at the end of the list:

['Tablet', 'Printer', 'Laptop', 'Monitor', 'Keyboard']

An Opposite Scenario

Sometimes, you may face an opposite situation, where you’ll need to convert a list to a DataFrame. If that’s the case, you may want to check the following guide that explains how to convert a list to a DataFrame in Python.

The .tolist() call will not update your structure in-place. Instead the method will return a new list, without modifying the original pd.Series object.

That means we must assign the result to the original variable to update it. However, if the original variable is a slice of a pd.DataFrame() we cannot do this, since the DataFrame will automatically convert a list to a pd.Series when assigning.

That means, doing numbers[2] = numbers[2].tolist() will still have numbers[2] being a pd.Series. To actually get a list, we need to assign the output to another (perhaps new) variable, that is not part of a DataFrame.

Thus, doing

numbers_list = numbers[2].tolist() print(type(numbers_list))

will output <class 'list'> as expected.

Pandas series can be converted to a list using tolist() or type casting method.

There can be situations when you want to perform operations on a list instead of a pandas object. In such cases, you can store the DataFrame columns in a list and perform the required operations. After that, you can convert the list back into a DataFrame.

In this article, you will learn how to use these methods to convert a pandas Series to a list followed by a few practical tips for using them.

ML+ Announcement [New]: Live Classes starts soon. Gain End-to-End Data Science skills by solving real Industry projects, with 1:1 mentor connects, live sessions & 24X7 support. Click here to Request call back

Creating a pandas Series

Let’s create a simple pandas series as an example.

import pandas as pd data_ser = {'Name': 'Sony', 'Country of Origin': 'Japan', 'Revenue': 25000000000} ser = pd.Series(data_ser) serName Sony Country of Origin Japan Revenue 25000000000 dtype: object

There are multiple other methods also to create a series apart from above.

How to use the tolist() method to convert pandas series to list

To convert a pandas Series to a list, simply call the tolist() method on the series which you wish to convert.

list_ser = ser.tolist() print('Created list:', list_ser) Created list: ['Sony', 'Japan', 25000000000]

Converting a DataFrame column to list

The columns of a pandas DataFrame are also pandas Series objects. You can convert the columns into a list using the tolist() method.

Let’s create a simple DataFrame to perform this operation as shown below.

data_df = {'Name': ['Sony', 'Tencent', 'Nintendo', 'Microsoft', 'Activision Blizzard'], 'Country of Origin': ['Japan', 'China', 'Japan', 'USA', 'USA'], 'Revenue': [25000000000, 13900000000, 12100000000, 11600000000, 8100000000]} df = pd.DataFrame(data_df) df
Series to list Python

After loading/creating the DataFrame, use the tolist() method on the selected column.

name_list = df['Name'].tolist() print('Created list:', name_list) print('Data type of the created list:', type(name_list)) Created list: ['Sony', 'Tencent', 'Nintendo', 'Microsoft', 'Activision Blizzard'] Data type of the created list: <class 'list'>

ML+ Announcement [New]: Live Classes starts soon. Gain End-to-End Data Science skills by solving real Industry projects, with 1:1 mentor connects, live sessions & 24X7 support. Click here to Request call back

Using the type casting method to convert series to list

Typecasting is the process of converting the data type of an object to another data type.

You can directly perform type casting to convert a series to a list in pandas. All you have to do is pass the series to a list() function

# Convert a pandas series to a list using type casting list_ser = list(ser) print('Created list:', list_ser) print('Object type of the created list:', type(list_ser)) Created list: ['Sony', 'Japan', 25000000000] Object type of the created list: <class 'list'>

You can also perform type casting to convert a DataFrame column to a list.
It is similar to using the tolist() method wherein you select the column which is to be converted and then typecast it to a list.

# Convert a DataFrame column to a list using type casting name_list = list(df['Name']) print('Created list:', name_list) print('Data type of the created list:', type(name_list)) Created list: ['Sony', 'Tencent', 'Nintendo', 'Microsoft', 'Activision Blizzard'] Data type of the created list: <class 'list'>

Practical Tips

  1. If you are converting a DataFrame column to a list, remember that you cannot convert it to a list and store it in the same variable. You need to store the list in a new variable.
  2. The tolist() method and the type casting methods work for pandas Series having nested objects as well.
  3. The type casting method can work on any iterable whereas the tolist() method will be used only on those iterables on which it is explicitly called.

Test Your Knowledge

Q1: The tolist() method can convert a DataFrame column to a list inplace. True or False?

Answer:

Answer: False. A new variable must be assigned to store the list.

Q2: You have a DataFrame df having columns col_1, col_2, and col_3. Write the code to convert col_2 to a list using the tolist() method.

Answer:

Answer: col_2_list = col_2.tolist()

Q3: Write the code to convert a pandas Series ser to a list using the type casting method.

Answer:

Answer: list_ser = list(ser)

Q4: Write the code to convert a pandas Series ser to a list using the tolist() method.

Answer:

Answer: list_ser = ser.tolist()

>

ML+ Announcement [New]: Live Classes starts soon. Gain End-to-End Data Science skills by solving real Industry projects, with 1:1 mentor connects, live sessions & 24X7 support. Click here to Request call back

This article was contributed by Shreyansh B and Shri Varsheni