Sling Academy
Home/Pandas/Pandas: How to get the list of values in a Series

Pandas: How to get the list of values in a Series

Last updated: February 17, 2024

Introduction

When working with data in Python, Pandas is an indispensable library that provides high-performance, easy-to-use data structures. One of these structures is the Series, a one-dimensional array capable of holding data of any type (integer, string, float, Python objects, etc.). This tutorial will guide you through the various ways to extract a list of values from a Pandas Series, with examples ranging from basic to advanced usage.

Getting Started

Before diving into the details, let us import the Pandas library. If you haven’t installed Pandas yet, you can do so using pip:

pip install pandas

And then import it in your Python script:

import pandas as pd

Basic Example

First, let’s start with the basic task of creating a Series and then converting it into a list.

s = pd.Series([1, 2, 3, 4, 5])
list_from_series = s.tolist()
print(list_from_series)

Output:

[1, 2, 3, 4, 5]

Using the values Attribute

Another way to convert a Series into a list is through the values attribute, which returns an array that can then be easily converted to a list.

s = pd.Series([10, 20, 30, 40, 50])
list_from_values = list(s.values)
print(list_from_values)

Output:

[10, 20, 30, 40, 50]

Dealing with Non-Numeric Data

Series in Pandas can also contain non-numeric data. Here’s how you handle those.

s = pd.Series(['apple', 'banana', 'cherry'])
fruit_list = s.tolist()
print(fruit_list)

Output:

['apple', 'banana', 'cherry']

Combining Series Into a List

Sometimes, you might work with multiple Series and need to combine them into a single list. Here’s how.

s1 = pd.Series([1, 2, 3])
s2 = pd.Series([4, 5, 6])
combined_list = s1.tolist() + s2.tolist()
print(combined_list)

Output:

[1, 2, 3, 4, 5, 6]

Advanced: Applying Conditions

If you need to filter values based on a condition before converting to a list, here is an example.

s = pd.Series([10, 20, 30, 40, 50])
filtered_list = s[s > 30].tolist()
print(filtered_list)

Output:

[40, 50]

Working with Date and Time Series

Date and time are common types of Series data. Converting them into lists requires an extra step due to the data type difference.

s = pd.Series(pd.date_range('2020-01-01', periods=5))
date_list = s.dt.to_pydatetime().tolist()
print([str(date) for date in date_list])

Output:

['2020-01-01 00:00:00', '2020-01-02 00:00:00', '2020-01-03 00:00:00', '2020-01-04 00:00:00', '2020-01-05 00:00:00']

Conclusion

In this tutorial, we have explored various ways to extract a list of values from a Pandas Series. Whether you’re working with numeric data, strings, dates, or need to apply specific conditions, Pandas offers versatile functionalities to easily convert Series to lists. Understanding these techniques can be extremely valuable for data manipulation and analysis tasks.

Next Article: Pandas: 4 Ways to Loop Through a Series

Previous Article: Pandas: How to get the list of indexes in a Series (4 ways)

Series: Pandas Series: From Basic to Advanced

Pandas

You May Also Like

  • How to Use Pandas Profiling for Data Analysis (4 examples)
  • How to Handle Large Datasets with Pandas and Dask (4 examples)
  • Pandas – Using DataFrame.pivot() method (3 examples)
  • Pandas: How to ‘FULL JOIN’ 2 DataFrames (3 examples)
  • Pandas: Select columns whose names start/end with a specific string (4 examples)
  • 3 ways to turn off future warnings in Pandas
  • How to Integrate Pandas with Apache Spark
  • How to Use Pandas for Web Scraping and Saving Data (2 examples)
  • How to Clean and Preprocess Text Data with Pandas (3 examples)
  • Pandas – Using Series.replace() method (3 examples)
  • Pandas json_normalize() function: Explained with examples
  • Pandas: Reading CSV and Excel files from AWS S3 (4 examples)
  • Using pandas.Series.rank() method (4 examples)
  • Pandas: Dropping columns whose names contain a specific string (4 examples)
  • Pandas: How to print a DataFrame without index (3 ways)
  • Fixing Pandas NameError: name ‘df’ is not defined
  • Pandas – Using DataFrame idxmax() and idxmin() methods (4 examples)
  • Pandas FutureWarning: ‘M’ is deprecated and will be removed in a future version, please use ‘ME’ instead
  • Pandas: Checking equality of 2 DataFrames (element-wise)