Built with Tutify.io

    AIStudyLabs

    How to Convert Pandas Column to List

    How to Convert Pandas Column to List

    You can get or convert the pandas DataFrame column to the list using Series.values.tolist(), since each column in DataFrame is represented as a Series internally, you can use this function after getting a column you wanted to convert as a Series. You can get a column as a Series by using df.column_name or df['column_name'].

    In this article, I will explain how to convert a column from a DataFrame to a list using the .tolist() method on the specific column.

    Key Points –

    <a id="h-1-quick-examples-of-convert-column-to-list"></a>

    1. Quick Examples of Convert Column to List

    Following are quick examples of converting column values to a list in DataFrame.

    
    # Quick examples of converting Pandas column to list 
    
    # Example 1: Using Series.values.tolist()
    col_list = df.Courses.values.tolist()
    
    # Example 2: Using Series.values.tolist()
    col_list = df["Courses"].values.tolist()
    
    # Example 3: Using list() Function
    col_list =  list(df["Courses"])
    
    # Example 4: Convert Pandas column to numpy array
    col_list = df['Courses'].to_numpy()
    
    # Example 5: Get the list by column index
    col_list = df[df.columns[0]].values.tolist()
    
    # Example 6: Convert index column to list
    index_list = df.index.tolist()
    

    Let’s create a Pandas DataFrame with a Python dictionary of lists, pandas DataFrame columns names Courses, Fee, Duration, Discount.

    
    # Create DataFrame
    import pandas as pd
    technologies = {
        'Courses':["Spark","PySpark","Python","Pandas","Python","Spark","Pandas"],
        'Fee' :[20000,25000,22000,30000,25000,20000,30000],
        'Duration':['30days','40days','35days','50days','40days','30days','50days'],
        'Discount':[1000,2300,1200,2000,2300,1000,2000]
                  }
    df = pd.DataFrame(technologies)
    print("Create DataFrame:\n", df)
    

    Yields below output.

    <a id="h-2-convert-the-pandas-column-to-a-list"></a>

    2. Convert the Pandas Column to a List

    You can use Series.values.tolist() to convert Pandas DataFrame Column to a list. df['Courses'] returns the DataFrame column as a Series and then uses this function to convert the column values to a list.

    Let's apply tolist() method to the specified column of DataFrame and get a Python list containing the values from that column.

    
    # Using Series.values.tolist() & get the list
    col_list = df.Courses.values.tolist()
    print("Get the list from DataFrame column:\n", col_list)
    

    Below is an explanation of each section of the statement df.Courses.values.tolist().

    Alternatively, you can also write the statement using.

    
    # Using Series.values.tolist()
    col_list = df["Courses"].values.tolist()
    

    **Related:**To get unique values in column as a list use the unique() function on the Series object.

    <a id="h-3-using-list-convert-pandas-column-to-list"></a>

    3. Using list() Convert Pandas Column to list

    You can also use the Python list() function to convert the Pandas column to a list. Simply, pass the specified column of DataFrame which we want to convert as a list into this function. It will return the list with the values of the specified column of DataFrame.

    
    # Using list() Function to get the list
    col_list =  list(df["Discount"])
    print("Get the list from specified column:\n", col_list)
    
    # Output:
    # Get the list from specified column:
    # [1000, 2300, 1200, 2000, 2300, 1000, 2000]
    

    <a id="h-4-convert-columns-to-numpy-array"></a>

    4. Convert Columns to Numpy Array

    Sometimes you may be required to convert the Pandas column to Numpy Array you can do so by using the to_numpy() function.

    
    # Conver to numpy.to_numpy() array
    col_list = df['Courses'].to_numpy()
    print("Get the array from specified column:\n", col_list)
    print(type(col_list))
    
    # Output:
    # Get the array from specified column
    # ['Spark' 'PySpark' 'Python' 'Pandas' 'Python'
    # 'Spark' 'Pandas']
    # <class 'numpy.ndarray'>
    

    <a id="h-5-get-list-by-column-index"></a>

    5. Get List by Column Index

    If you have a column index and want to get the column values of an index, first you have toget the column name by index and then use the approaches explained above to convert it to a list. From the below example df.columns[0] returns the column name for an index 0, which is Courses.

    
    # Get by Column Index
    col_list = df[df.columns[0]].values.tolist()
    print("Get the list from specified column:\n", col_list)
    
    # OUtput: 
    # Get the list from specified column:
    # ['Spark', 'PySpark', 'Python', 'Pandas', 'Python', 'Spark', 'Pandas']
    

    <a id="h-6-convert-the-index-column-to-a-list"></a>

    6. Convert the Index Column to a List

    In case you want to get the index column as a list use DataFrame.index.tolist() method. Even here DataFrame.index returns the Index object and tolist() converts it to list.

    
    # Convert Index Column to List
    index_list = df.index.tolist()
    print(index_list)
    
    # Output:
    # [0, 1, 2, 3, 4, 5, 6]
    

    <a id="h-7-complete-example-of-pandas-convert-column-to-list"></a>

    7. Complete Example of Pandas Convert Column to List

    
    import pandas as pd
    import numpy as np
    technologies = {
        'Courses':["Spark","PySpark","Python","Pandas","Python","Spark","Pandas"],
        'Fee' :[20000,25000,22000,30000,25000,20000,30000],
        'Duration':['30days','40days','35days','50days','40days','30days','50days'],
        'Discount':[1000,2300,1200,2000,2300,1000,2000]
                  }
    df = pd.DataFrame(technologies)
    
    # Using Series.values.tolist()
    col_list = df.Courses.values.tolist()
    
    # Using Series.values.tolist()
    col_list = df["Courses"].values.tolist()
    
    # Using list() function
    col_list =  list(df["Courses"])
    
    # Get by Column Index
    col_list = df[df.columns[0]].values.tolist()
    
    # Convert Index Column to List
    index_list = df.index.tolist()
    

    <a id="h-frequently-asked-questions-on-pandas-convert-column-to-list"></a>

    Frequently Asked Questions on Pandas Convert Column to List

    You can convert a specific column in a pandas DataFrame to a list by using the .tolist() method on the column.

    Once you've converted a column to a list, you can perform various operations on it like any other Python list.

    By default, the .tolist() method will convert the column to a list with the same data type as the column's data. If you want to enforce a specific data type, you can use list comprehensions to cast the data to the desired type

    Yes, you can convert an entire DataFrame to a list of lists using the .values.tolist() method. Each row in the DataFrame will be converted to a list, and the list of lists represents the entire DataFrame.

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

    Conclusion

    In this article, you have learned how to get the column values as a list by using several methods like Series.values, list(), and also learned to convert the index column to a list with examples.

    Happy Learning !!

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

    Related Articles

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

    References