Python Forum
How to add columns to polars dataframe
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to add columns to polars dataframe
#1
I am using polars to read an xlsx sheet in a dataframe.

How can I add columns to the start of the worksheet? Since using using "with_colums" add columns to the datafram but these are added to the of the sheet
Reply
#2
Well, can you try the below code I hope you can get the result you are looking for.

Quote:import polars as pl

# Create a sample DataFrame
data = {
"A": [1, 2, 3],
"B": [4, 5, 6]
}

df = pl.DataFrame(data)

# Create new columns
new_columns = {
"X": [7, 8, 9],
"Y": [10, 11, 12]
}

# Insert the new columns at the start of the DataFrame
index_to_insert = 0 # Insert at the start
df = df.insert_at_idx(index_to_insert, new_columns)

# Print the updated DataFrame
print(df)
Thanks
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to count total number of sheets in an excel workbook using polars sayyedkamran 0 683 Oct-27-2023, 09:54 PM
Last Post: sayyedkamran
  concat 3 columns of dataframe to one column flash77 2 855 Oct-03-2023, 09:29 PM
Last Post: flash77
  Convert several columns to int in dataframe Krayna 2 2,426 May-21-2021, 08:55 AM
Last Post: Krayna
  Outputs "NaN" after "DataFrame columns" function? epsilon 7 3,695 Jan-27-2021, 10:59 AM
Last Post: epsilon
  Adapting a dataframe to the some of columns flyway 2 2,077 Aug-12-2020, 07:21 AM
Last Post: flyway
  Difference of two columns in Pandas dataframe zinho 2 3,383 Jun-17-2020, 03:36 PM
Last Post: zinho
  DataFrame: To print a column value which is not null out of 5 columns mani 2 2,136 Mar-18-2020, 06:07 AM
Last Post: mani
Question Dividing a single column of dataframe into multiple columns based on char length darpInd 2 2,485 Mar-14-2020, 09:19 AM
Last Post: scidam
  Interate for loop over certain columns in dataframe Finpyth 2 1,973 Mar-06-2020, 08:34 AM
Last Post: Finpyth
  How to highlight dataframe columns SriRajesh 1 1,874 Feb-28-2020, 11:02 PM
Last Post: Marbelous

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020