Skip to main content

Pandas column manipulation toolkit

Project description

smartcols

Utilities for reordering and grouping pandas DataFrame columns without the usual index gymnastics. smartcols keeps column manipulations readable and testable while exposing a single API that works with pure or in-place workflows.

Features

  • Swap, move before/after, push to front/end, or reorder multiple columns at once
  • sort_columns with several strategies (default, variance, std_dev, nan_ratio, correlation, mean, custom)
  • Column grouping helpers (dtype, NaN ratio buckets, regex, metadata maps, custom keys) and flatten helpers
  • Optional inplace=True on every mutator to avoid extra copies without breaking method chains via DataFrame.pipe
  • Typed API with validation and descriptive error messages for faster debugging

Installation

pip install smartcols

Quick start

import pandas as pd
from smartcols import (
    swap_columns,
    move_after,
    move_before,
    move_to_front,
    move_to_end,
    sort_columns,
    group_columns,
)

# Base DataFrame
df = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6], "C": [7, 8, 9], "D": [10, 11, 12]})

# Swap columns
swap_df = swap_columns(df, "A", "D")

# Move multiple columns after a target
reordered = move_after(df, ["B", "C"], "A")

# Update in place
move_to_front(df, "D", inplace=True)

# Combine with pipe for readable pipelines
pipe_df = (
    df.copy()
      .pipe(move_to_front, ["C", "A"])
      .pipe(move_after, "B", "C")
)

# Sort by variance
variance_sorted = sort_columns(df, by="variance")

# Group by NaN ratio buckets
nan_groups = group_columns(
    pd.DataFrame({
        "good": [1, 2, 3],
        "ok": [1, None, 3],
        "bad": [None, None, 1],
        "terrible": [None, None, None],
    }),
    by="nan_ratio",
)

API overview

Function Description
swap_columns(df, col1, col2, *, inplace=False) Swap two columns by name
move_after(df, cols, target, *, inplace=False) Move one or more columns immediately after target
move_before(df, cols, target, *, inplace=False) Move columns immediately before target
move_to_front(df, cols, *, inplace=False) Push column(s) to the front
move_to_end(df, cols, *, inplace=False) Push column(s) to the end
sort_columns(df, by='default', order='ascending', target='', key=None) Reorder columns by alphabetical order, NaN ratio, variance, std dev, correlation, mean, or custom key
group_columns(df, by, ...) Split columns into groups based on dtype, NaN ratio buckets, regex patterns, metadata map, or custom key
group_columns_flattened(...) Same as group_columns but returns a single DataFrame with grouped segments stacked
save_column_order(df) / apply_column_order(df, order) Capture column order snapshots and reapply them

Every mutating helper returns the reordered DataFrame unless inplace=True, in which case it returns None and the supplied DataFrame is updated.

Testing

pip install -e .[tests]
pytest

(If pytest complains about temp directories in constrained environments, set TMPDIR to a writable location first.)

Versioning

smartcols follows semantic versioning. Current release is 0.1.0.

License

MIT License. See LICENSE for details.

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

pandas_smartcols-0.1.0.tar.gz (9.7 kB view details)

Uploaded Source

Built Distribution

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

pandas_smartcols-0.1.0-py3-none-any.whl (8.2 kB view details)

Uploaded Python 3

File details

Details for the file pandas_smartcols-0.1.0.tar.gz.

File metadata

  • Download URL: pandas_smartcols-0.1.0.tar.gz
  • Upload date:
  • Size: 9.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for pandas_smartcols-0.1.0.tar.gz
Algorithm Hash digest
SHA256 d6ac614c90d9c141f321fcf51dd29f43967db775cb2f26f309ca142463371811
MD5 57ebcd0d0bddc9c72c5c5ce9c090d0dc
BLAKE2b-256 1255ad3fb7a4e5cd99c73f9b33a7c4dfea8c692663639bef9e90fc153c94d6a6

See more details on using hashes here.

File details

Details for the file pandas_smartcols-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pandas_smartcols-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a678599404f14d0419c8887ee940e9a5ebbe33cb82153b7eab71f50d0f52e5fb
MD5 20130fff56b47a9fcaffc2d468f0bfa9
BLAKE2b-256 59bde87f44d76b0b9bce43adf4cbfc20b602940a44523263a04c75c113c84b19

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