Skip to main content

DataFrame column reordering made simple

Project description

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:

  1. Computes the new column order (pure list manipulation)
  2. Calls df.select(new_order) via narwhals

That's it. Clean and simple.

License

MIT

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

colocate-0.1.2.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

colocate-0.1.2-py3-none-any.whl (8.7 kB view details)

Uploaded Python 3

File details

Details for the file colocate-0.1.2.tar.gz.

File metadata

  • Download URL: colocate-0.1.2.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using 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}

File hashes

Hashes for colocate-0.1.2.tar.gz
Algorithm Hash digest
SHA256 7648d227224b593f6a603d26b5f0c98e51029a4f685b182b6b85a718864fa558
MD5 722ab0134fa3008142eabf1f5a82da93
BLAKE2b-256 13d3f094746440092672dd081e6ffd10554085a4ce5d510f155bede37945ed65

See more details on using hashes here.

File details

Details for the file colocate-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: colocate-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 8.7 kB
  • Tags: Python 3
  • Uploaded using 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}

File hashes

Hashes for colocate-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 97a43a64ef8ae57340f08ced930defa54b4e332e1f107e55a64c726f7d52ee5a
MD5 7a2b5b2a26cb75df4c53aad40462d008
BLAKE2b-256 e083f62d8154a9a526fe45265b6399a12d5372926cf564b6b220e2a3fe5bc3d7

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page