Pandas Series.to_list()
Pandas Series.to_list() Tutorial
In this tutorial, we'll explore the Series.to_list()
method in Pandas, which is used to convert a Pandas Series into a Python list, with well detailed example programs.
Syntax
The syntax of Series.to_list()
is straightforward, with no additional parameters:
Series.to_list()
The method returns a Python list containing the values of the Pandas Series.
This method is similar to Series.tolist().
Examples for Series.to_list()
1. Convert Series to Python List
In this example, we'll use Series.to_list()
to convert a Pandas Series into a Python list.
Python Program
import pandas as pd
# Create a Series
series = pd.Series({'Name': 'Alice', 'Age': 25, 'City': 'New York'})
# Convert the Series to a Python list
python_list = series.to_list()
# Print the original Series and the resulting Python list
print("Original Series:")
print(series)
print("\nResulting Python List:")
print(python_list)
Output
Original Series:
Name Alice
Age 25
City New York
dtype: object
Resulting Python List:
['Alice', 25, 'New York']
2. Convert Numeric Series to Python List
In this example, we'll use Series.to_list()
to convert a numeric Pandas Series into a Python list.
Python Program
import pandas as pd
# Create a numeric Series
numeric_series = pd.Series([10, 20, 30, 40, 50])
# Convert the numeric Series to a Python list
numeric_list = numeric_series.to_list()
# Print the original Series and the resulting Python list
print("Original Numeric Series:")
print(numeric_series)
print("\nResulting Python List:")
print(numeric_list)
Output
Original Numeric Series:
0 10
1 20
2 30
3 40
4 50
dtype: int64
Resulting Python List:
[10, 20, 30, 40, 50]
Summary
In this tutorial, we've covered the Series.to_list()
method in Pandas, which allows you to easily convert a Pandas Series into a Python list. This can be useful when you need to work with the data in list format or pass it to functions that expect a list.