colocate
DataFrame column reordering made simple. Works with pandas and polars.
Installation
uv add colocate
or
pip install colocate
Usage
Just import to register df.relocate():
import polars as pl
import colocate # Registers df.relocate() and pl.between()
df = pl.DataFrame({
"age": [25, 30, 35],
"city": ["NYC", "LA", "Chicago"],
"id": [1, 2, 3],
"name": ["Alice", "Bob", "Charlie"],
"score": [85, 90, 78],
})
Move to first (default)
df.relocate("id")
# ['id', 'age', 'city', 'name', 'score']
df.relocate(["id", "name"])
# ['id', 'name', 'age', 'city', 'score']
Move to last
df.relocate("score", to="last")
# ['age', 'city', 'id', 'name', 'score']
df.relocate(["score", "age"], to="last")
# ['city', 'id', 'name', 'score', 'age']
Move after anchor
df.relocate("name", after="id")
# ['age', 'city', 'id', 'name', 'score']
df.relocate(["name", "age"], after="id")
# ['city', 'id', 'name', 'age', 'score']
Chaining
For complex reordering, chain multiple calls:
(df
.relocate("id")
.relocate("name", after="id")
.relocate("score", to="last")
)
# ['id', 'name', 'age', 'city', 'score']
Column Ranges with pl.between()
For sequential columns (common in survey data), use pl.between():
df = pl.DataFrame({
"respondent_id": [1],
"Q1_1": [1], "Q1_2": [2], "Q1_3": [3],
"Q2_1": [4], "Q2_2": [5],
"weight": [1.0],
})
df.relocate(pl.between("Q2_1", "Q2_2"))
# ['Q2_1', 'Q2_2', 'respondent_id', 'Q1_1', 'Q1_2', 'Q1_3', 'weight']
df.relocate(pl.between("Q2_1", "Q2_2"), after="respondent_id")
# ['respondent_id', 'Q2_1', 'Q2_2', 'Q1_1', 'Q1_2', 'Q1_3', 'weight']
df.relocate(pl.between("Q1_1", "Q1_3"), to="last")
# ['respondent_id', 'Q2_1', 'Q2_2', 'weight', 'Q1_1', 'Q1_2', 'Q1_3']
Polars Selectors
Full support for polars selectors:
import polars.selectors as cs
df.relocate(cs.last()) # last column → first
df.relocate(cs.last(), after="id") # last column → after id
df.relocate(cs.numeric(), to="last") # all numeric → end
df.relocate(cs.string()) # all string → first
df.relocate(cs.matches("^Q1_")) # regex match → first
df.relocate(cs.starts_with("Q2")) # prefix match → first
df.relocate(cs.by_name("score", "name")) # specific cols → first
Works with pandas too
import pandas as pd
import colocate
df = pd.DataFrame({...})
df.relocate("id")
df.relocate(["score"], to="last")
df.relocate("name", after="id")
Note: pl.between() is polars-only. For pandas, use explicit column lists.
API
df.relocate(columns, after=None, to=None)
| Parameter | Type | Description |
|---|---|---|
columns |
str | list[str] | Between |
Column(s) to move. Use pl.between(start, end) for ranges. |
after |
str | None |
Place columns after this anchor column. |
to |
"first" | "last" | None |
Position shortcut. Default is "first". |
Note: after and to are mutually exclusive.
pl.between(start, end) # Select columns from start to end (inclusive)
How it works
Under the hood, colocate simply:
- Computes the new column order (pure list manipulation)
- Calls
df.select(new_order)via narwhals
That's it. Clean and simple.
License
MIT
Metadata
Release files for colocate 0.1.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| colocate-0.1.2.tar.gz | 4.9 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| colocate-0.1.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 13.6 kB
Release files / colocate-0.1.2.tar.gz
| Download URL | colocate-0.1.2.tar.gz |
|---|---|
| Size | 4.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
7648d227224b593f6a603d26b5f0c98e51029a4f685b182b6b85a718864fa558
|
|
BLAKE2b-256 checksum How to use checksums |
13d3f094746440092672dd081e6ffd10554085a4ce5d510f155bede37945ed65
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.9.15 {"installer":{"name":"uv","version":"0.9.15","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|
Release files / colocate-0.1.2-py3-none-any.whl
| Download URL | colocate-0.1.2-py3-none-any.whl |
|---|---|
| Size | 8.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
97a43a64ef8ae57340f08ced930defa54b4e332e1f107e55a64c726f7d52ee5a
|
|
BLAKE2b-256 checksum How to use checksums |
e083f62d8154a9a526fe45265b6399a12d5372926cf564b6b220e2a3fe5bc3d7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.9.15 {"installer":{"name":"uv","version":"0.9.15","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|