A Polars plugin for easily reordering DataFrame columns
Project description
Polars Permute Plugin
A Polars plugin for easily reordering DataFrame columns.
Supports column permutations like prepending, appending, shifting, and swapping.
Installation
pip install polars-permute[polars]
On older CPUs run:
pip install polars-permute[polars-lts-cpu]
Features
- Supports both string column names and Polars expressions
- Handles single or multiple columns
- Maintains relative ordering of moved columns
- Chain operations together
- Gracefully handles edge cases (non-existent columns, empty inputs)
Usage
The plugin adds a permute namespace to Polars DataFrames with methods for column reordering:
import polars as pl
import polars_permute
# Create a sample DataFrame
df = pl.DataFrame({
"a": [1, 2, 3],
"b": [4, 5, 6],
"c": [7, 8, 9],
"d": [10, 11, 12]
})
# Move column 'd' to the start
df.permute.prepend("d")
# Move multiple columns to the end
df.permute.append(["a", "b"])
# Move columns to a specific position
df.permute.at(["b", "c"], index=0)
# Shift columns left/right
df.permute.shift("a", "b", steps=1, direction="right")
# Swap two columns
df.permute.swap("a", "d")
API Reference
prepend(cols)
Move specified column(s) to the start (index 0).
df.permute.prepend("d") # Single column
df.permute.prepend(["c", "d"]) # Multiple columns
df.permute.prepend(pl.col("a").alias("x")) # Using expressions
append(cols)
Move specified column(s) to the end.
df.permute.append("a")
df.permute.append(["a", "b"])
at(cols, index)
Move specified column(s) to exact position.
df.permute.at("d", 1) # Move 'd' to index 1
df.permute.at(["b", "c"], 0) # Move multiple columns
shift(cols, steps=1, direction="left")
Shift column(s) left or right by steps.
df.permute.shift("c", steps=1, direction="left")
df.permute.shift("a", "b", steps=2, direction="right")
swap(col1, col2)
Swap positions of two columns.
df.permute.swap("a", "d")
Notes
- Operations create a new DataFrame; the original is not modified
- Column order is preserved for multiple column operations
- Invalid columns are ignored gracefully
- Out-of-bounds indexes are clamped to valid range
Contributing
Feel free to open issues or submit pull requests for improvements or bug fixes.
License
MIT License
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file polars_permute-0.1.2.tar.gz.
File metadata
- Download URL: polars_permute-0.1.2.tar.gz
- Upload date:
- Size: 5.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: pdm/2.22.3 CPython/3.10.6 Linux/5.15.0-125-generic
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
18c79b3153c2e64149dc86dac5218a0bf7ad36361750e0c2191c1dd5724fa5bf
|
|
| MD5 |
11ad967a41a14da8416d467ec95f1a5b
|
|
| BLAKE2b-256 |
32e7c6119b612f8025be6d26fedfcd4c4baf20201abbfc9c76e2f444908e4b19
|
File details
Details for the file polars_permute-0.1.2-py3-none-any.whl.
File metadata
- Download URL: polars_permute-0.1.2-py3-none-any.whl
- Upload date:
- Size: 3.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: pdm/2.22.3 CPython/3.10.6 Linux/5.15.0-125-generic
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
680b8bdfd435c1ba961a0b0b6b3e9e3b957f66efe7b96d49ad6f9955efdbee3f
|
|
| MD5 |
34095588b622efd6fd21ff014211fbf8
|
|
| BLAKE2b-256 |
fe772a9039ba82ed2c46384f2576570b7c6bc1f524a0b4bcfd795da42649b76a
|