How to Convert Series to List in Pandas

To convert a Pandas Series to a list, you can use the “tolist()” method.

Example

import pandas as pd

# Sample DataFrame
data = {
  "A": [11, 21, 31, 41, 51],
  "B": [10, 20, 30, 40, 50]
}

df = pd.DataFrame(data)

print("Original DataFrame:")
print(df)

# Select a Series from the DataFrame
series_a = df["A"]

print("\nSeries A:")
print(series_a)

# Convert the Series to a list
list_a = series_a.tolist()

print("\nList A:")
print(list_a)

Output

Original DataFrame:
    A   B
0  11   10
1  21   20
2  31   30
3  41   40
4  51   50

Series A:
0   11
1   21
2   31
3   41
4   51
Name: A, dtype: int64

List A:
[11, 21, 31, 41, 51]

In this code, we created a sample df with columns “A” and “B”.

We selected the “A” column as a Series and then use the tolist() method to convert it to a list.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.