Menu
Stuff by Yuki
  • Home
  • Data Engineering
    • Python
  • Business Intelligence
    • Power BI
    • Tableau
  • Perspectives
  • About
  • Contact
Stuff by Yuki

Convert DataFrame to Series in Polars

Posted on February 21, 2023February 21, 2023

There are basically 2 ways to do this.

  1. Use .to_series() on .select() method on your polars dataframe.
  2. Use .to_series() alone. And you specify the index of your column to return. The first column by default.
  3. Choose a specific column like you do in pandas, with code like this: df[‘Your Column’]

Some may think that df.select(‘Your Column’) alone would work but nope. It doesn’t return a series object, it returns a dataframe object instead.

Copy Copied Use a different Browser

import polars as pl

# creating dummy data
data = [[1, 2, 3], ['A', 'B', 'C'], [111, 222, 333]]
df = pl.DataFrame(data, schema=['ID', 'Letter', 'Values'])
print(type(df))

# 1. using .to_series() on .select()
series_1 = df.select('ID').to_series()
print(type(series_1))

# 2. using .to_series() alone, column index specified
series_2 = df.to_series(2)
print(type(series_2))

# 3. just taking a column as a series
series_3 = df['ID']
print(type(series_3))

# .select() itself returns a dataframe
this_is_df = df.select('ID')
print(type(this_is_df))

Source code: Github repo

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • Where I’m Headed in the Next 5 Years
  • Open-Source vs Vendor Data Tools
  • Developing the Habit of Writing
  • How to Inspect and Optimize Query Plans in Python Polars
  • Learn Python Polars with Polars Cookbook

Popular Posts

  • A Running Total Calculation with Quick Measure in Power BI
  • A Complete Guide to Git Integration in Power BI
  • How To Copy And Paste Report Page in Power BI
  • Handling Missing Values in Polars
  • How to Convert String to Date or Datetime in Polars

connect with me

  • LinkedIn
  • Twitter
  • Github
  • Website

Search Articles

©2025 Stuff by Yuki | Powered by SuperbThemes