It seems exclude() needs column's name and you can use df.columns[1] to get its name.
index = 1
result = df.select(pl.exclude( df.columns[index] ))
result = df.drop( df.columns[index] )
It seems works also:
result = df.drop( pl.nth(index) )
Minimal working code for tests:
It allows to run ie. main.py 2 to remove 3rd column.
import polars as pl
import sys
# Creating a new Polars DataFrame
technologies = {
'Courses': ["Spark", "Pandas", "Hadoop", "Python", "Pandas", "Spark"],
'Fees': [22000, 26000, 25000, 20000, 26000, 22000],
'Duration': ['30days', '60days', '50days', '40days', '60days', '30days'],
'Discount': [1000, 2000, 1500, 1200, 2000, 1000]
}
df = pl.DataFrame(technologies)
print(df)
if len(sys.argv) > 1:
index = int(sys.argv[1])
else:
index = 1
print('index:', index)
name = df.columns[index]
print('name:', name)
# --- version 1 ---
print('--- version 1 ---')
result = df.select([col for idx, col in enumerate(df.columns) if idx != index])
print(result)
#result = df.select(pl.exclude(pl.nth(1)))
# --- version 2 ---
print('--- version 2 ---')
#result = df.select(pl.exclude(df.columns[index]))
name = df.columns[index]
result = df.select(pl.exclude(name))
print(result)
# --- version 3 ---
print('--- version 3 ---')
#result = df.drop(df.columns[index])
name = df.columns[index]
result = df.drop(name)
print(result)
# --- version 4 ---
print('--- version 4 ---')
result = df.drop(pl.nth(index))
print(result)
# ---
#print(df)
minimal working codeso we could use it for tests.df.select(pl.exclude(df.columns[1]))