Built with Tutify.io

    AIStudyLabs

    Series.tolist() - Convert Pandas Series to List

    Series.tolist() - Convert Pandas Series to List

    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.

    Key Points -

    <a id="h-quick-examples-to-convert-series-to-list"></a>

    Quick Examples to Convert Series to List

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

    
    # Quick examples to convert series to list
    
    # 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'])
    

    <a id="h-syntax-of-pandas-series-tolist"></a>

    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.

    <a id="h-create-series-from-dictionary"></a>

    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("Pandas Series:\n",s)
    

    Yields below output.

    <a id="h-usage-of-pandas-series-tolist"></a>

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

    <a id="h-convert-dataframe-column-series-to-list"></a>

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

    <a id="h-use-type-casting-method-to-convert-series-to-list"></a>

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

    <a id="h-frequently-asked-questions-on-series-to-list"></a>

    Frequently Asked Questions on Series to List

    To convert a Pandas Series to a Python list, you can use either the tolist() method of the Pandas Series or the built-in list() function.

    Besides tolist(), you can also use the built-in list() function to convert a Pandas Series to a Python list.

    Both methods (tolist() and list()) achieve the same result, converting a Pandas Series to a Python list. However, the tolist() method is a Pandas-specific method, while the list() function is a built-in Python function. In most cases, both methods are interchangeable, and you can choose the one that you find more convenient or readable.

    You can convert a specific column of a Pandas DataFrame to a Python list. You can achieve this by accessing the column using its name and then using the tolist() method or the list() function.

    If your Pandas Series contains missing values (NaN), the resulting list will also contain them. The NaN values will be represented as None in the list.

    The order of elements in the Pandas Series will be preserved when converting it to a list. Both the tolist() method and the list() constructor maintain the order of elements.

    <a id="h-conclusion"></a>

    Conclusion

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

    <a id="h-related-articles"></a>

    Related Articles

    <a id="h-references"></a>

    References