Menu
Stuff by Yuki
  • Home
  • Python
  • Power BI
  • Tableau
  • Community
    • Makeover Monday
    • Workout Wednesday
  • 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

  • What are Power BI Dataflows?
  • Calculate the Max Sales Amount for Product
  • Convert DataFrame to Series in Polars
  • Pandas vs Polars – Speed Comparison
  • Polars with DuckDB – Using SQL in Polars

Popular Posts

  • A Running Total Calculation with Quick Measure in Power BI
  • How To Copy And Paste Report Page in Power BI
  • Year-Over-Year Calculation: Time Intelligence in Power BI
  • How to Fill Dates Between Start Date and End Date in Power BI (Power Query)
  • Network Visualizations in Python

connect with me

  • LinkedIn
  • Twitter
  • Github
©2023 Stuff by Yuki | Powered by SuperbThemes & WordPress