Built with Tutify.io

    AIStudyLabs

    Convert DataFrame to Series in Polars

    Convert DataFrame to Series in Polars

    In Polars, you can convert a DataFrame to a Series by selecting a specific column. Each column in a Polars DataFrame is inherently a Series. A DataFrame in Polars is a two-dimensional structure with rows and columns, similar to a table, whereas a Series is a one-dimensional structure representing a single column of data. Since a DataFrame cannot be directly converted into a Series, the typical approach is to extract an individual column from the DataFrame. In this article, I will explain how to convert a DataFrame to a Series in Polars.

    Key Points –

    <a id="h-usage-of-convert-polars-dataframe-to-series"></a>

    Usage of Convert Polars DataFrame to Series

    In Polars, a DataFrame is a collection of Series (columns), and converting a specific column from a DataFrame to a Series is a common operation.

    <a id="h-what-is-a-series-in-polars"></a>

    What is a Series in Polars?

    A Series in Polars is a single column of data with a specific data type (e.g., integers, strings, floats). It is similar to a pandas Series or a single column in a spreadsheet.

    To run some examples of converting polars DataFrame to Series, let’s create a Polars DataFrame.

    
    import polars as pl
    
    technologies= ({
        'Courses':["spark","python","spark","python","pandas"],
        'Fees' :[22000,25000,22000,25000,24000],
        'Duration':['30days','40days','60days','40days','50days'],
        'Discount':[1000,1500,1000,2000,2500]
                  })
    df = pl.DataFrame(technologies)
    print("Original DataFrame:\n", df)
    

    Yields below output.

    You can convert a single column of a Polars DataFrame into a Series using square bracket notation (df["column_name"]). This method allows you to access a column by its name, returning it as a Series.

    
    # Convert 'Courses' column to a Series
    df2 = df["Courses"]
    print("Courses Series:\n", df2)
    

    Here,

    <a id="h-convert-a-column-by-index"></a>

    Convert a Column by Index

    You can convert a column to a Series by referencing its index position in the DataFrame. This is useful when the column name is unknown but its position is known. Instead of using the column name, you can access and convert the column using its index.

    
    # Convert the first column (index 0) to a Series
    df2 = df[:, 1] 
    print("Column Series:\n", df2)
    
    # Output:
    # Column Series:
     shape: (5,)
    Series: 'Fees' [i64]
    [
    	22000
    	25000
    	22000
    	25000
    	24000
    ]
    

    Here,

    <a id="h-convert-a-column-with-select-method"></a>

    Convert a Column with Select() Method

    You can use the select() method to choose a specific column from a Polars DataFrame and then convert it to a Series using the to_series() method. This approach is useful when you want to work with the select() method for column selection and then explicitly convert the result to a Series.

    
    # Convert the 'Fees' column to a Series 
    # using select() function
    df2 = df.select("Fees").to_series()
    print("Fees Series:\n", df2)
    

    Here,

    Yields the same output as above.

    <a id="h-convert-a-column-with-get-column-method"></a>

    Convert a Column with get_column() Method

    The get_column() method provides a simple way to extract a specific column from a DataFrame as a Series. It explicitly retrieves a column by name and returns it as a Series. You can use get_column() to convert a column into a Series in Polars.

    
    # Convert the 'Discount' column 
    # To a Series using get_column()
    df2 = df.get_column("Discount")
    print("Discount Series:\n", df2)
    
    # Output:
    # Discount Series:
     shape: (5,)
    Series: 'Discount' [i64]
    [
    	1000
    	1500
    	1000
    	2000
    	2500
    ]
    

    Here,

    <a id="h-convert-a-column-with-to-series-method"></a>

    Convert a Column with to_series() Method

    You can use the to_series() method to convert a column from a polars DataFrame into a Series. This method is typically used after selecting a column or when you already have a reference to a column.

    
    # Convert the 'Duration' column to a Series using to_series()
    df2 = df.select("Duration").to_series()
    print("Duration Series:\n", df2)
    
    # Output:
    # Duration Series:
     shape: (5,)
    Series: 'Duration' [str]
    [
    	"30days"
    	"40days"
    	"60days"
    	"40days"
    	"50days"
    ]
    

    Here,

    <a id="h-extracting-and-performing-element-wise-operations"></a>

    Extracting and Performing Element-wise Operations

    You can extract a column as a Series and apply element-wise operations using vectorized functions. These operations are performed on each element of the Series individually, allowing efficient computations.

    
    # Extract the 'Fees' column as a Series
    fees_series = df.get_column("Fees")
    
    # Perform element-wise operation: Apply a 10% discount
    df2 = fees_series * 0.9
    print("Discounted Fees:\n", df2)
    
    # Output
    # Discounted Fees:
     shape: (5,)
    Series: 'Fees' [f64]
    [
    	19800.0
    	22500.0
    	19800.0
    	22500.0
    	21600.0
    ]
    

    Here,

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

    Conclusion

    In summary, converting a Polars DataFrame column into a Series is a simple and efficient process that enables flexible data manipulation. Whether using bracket notation (df["column_name"]), get_column(), or to_series(), each method offers an optimized approach to extracting and working with individual columns.

    Happy Learning!!

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

    Related Articles

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

    References