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.1.tar.gz (9.8 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.1-py3-none-any.whl (8.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pandas_smartcols-0.1.1.tar.gz
  • Upload date:
  • Size: 9.8 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.1.tar.gz
Algorithm Hash digest
SHA256 bce701e55d22cc110f8f8a71d2d7b7a86e2ef98a4e64dc3c8d15956b37dd5ef5
MD5 0560a14ad6f37b432dc1f880f96e6d9d
BLAKE2b-256 3c9d53c16a6ba676d9a983cef132f9474fd223d764e4b99eaf2cd9a9408f584b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pandas_smartcols-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 40dcaeb7a597f823d5b5c4b08b1d092bf6c06c37f4522b6c3f56efebe1467839
MD5 e9863b04e8bb33e20e24cd523cd23dcc
BLAKE2b-256 6b4f4f344ae1390633ff152e1e88d210044bcdec1cbc86966b2c1f0ef5bb2286

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