Skip to main content

DataFrame column reordering made simple

Project description

colocate

DataFrame column reordering made simple. Works with pandas and polars.

Installation

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.1.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.1-py3-none-any.whl (8.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: colocate-0.1.1.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.1.tar.gz
Algorithm Hash digest
SHA256 ef060c1b503f454997d234bda05f446b062f7ad27429cc31a7677cc25d7c2ce6
MD5 a4bf006d9ae7829a65b624ce43bc3505
BLAKE2b-256 f03dc30d68f4a49f9a1b204d9f53054baa736aacf40d748ec4294add083541d0

See more details on using hashes here.

File details

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

File metadata

  • Download URL: colocate-0.1.1-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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1a1e00e493f69c67f0fa4da1de44bf8888c07470d2ba2f1d5c08e2976219e79c
MD5 d392088f4f73561a36e96a11067e3781
BLAKE2b-256 56e595e5f1e9781e69caf8101109e8f87ad97593b36eb089317bf4e5736dd4b7

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