Simple, composable column selector for loc[], iloc[], assign() and others.
Project description
Pandas Selector
Simple, composable selectors for loc[], iloc[], assign() and others.
Overview
Motivation: Make chaining Pandas operations easier and bring functionality to Pandas similar to Spark’s col() function or referencing columns in R’s dplyr.
Install from PyPI with pip install pandas-selector. Pandas versions 1.0+ (^1.0) are supported.
Documentation can be found at readthedocs.
Source code can be obtained from GitHub.
Example: Create new column and filter
Instead of writing “traditional” Pandas like this:
df_in = pd.DataFrame({"x": range(5)})
df = df_in.assign(y = df_in.x // 2)
df = df.loc[df.y <= 1]
df
# x y
# 0 0 0
# 1 1 0
# 2 2 1
# 3 3 1
One can write:
from pandas_selector import DF
df = (df_in
.assign(y = DF.x // 2)
.loc[DF.y <= 1]
)
This is especially handy when re-iterating on data frame manipulations interactively, e.g. in a notebook.
But you can access all methods and attributes of the data frame from the context:
df = pd.DataFrame({
"X": range(5),
"Y": ["1", "a", "c", "D", "e"],
})
df.loc[DF.y.str.isupper() | DF.y.str.isnumeric()]
# X y
# 0 0 1
# 3 3 D
df.loc[:, DF.columns.str.isupper()]
# X
# 0 0
# 1 1
# 2 2
# 3 3
# 4 4
Similar projects for pandas
-
(+) active
(-) new API to learn
-
(-) stale(?), last change 6 years ago
(-) new API to learn
(-) Symbol / pandas_ply.X works only with ply_* functions
-
(+) no explicite df necessary
(-) new API to learn
-
(+) simple select accessor
(-) usage inside chains clumsy (needs explicite df):
((df .select.A == 'a') .select.B == 'b' )
(-) hard-coded str, dt accessor methods
(?) composable?
Development
Development is containerized with [Docker](https://www.docker.com/) to separte from host systems and improve reproducability. No other prerequisites are needed on the host system.
Recommendation for Windows users: install WSL 2 (tested on Ubuntu 20.04), and for containerized workflows, Docker Desktop for Windows.
The common tasks are collected in Makefile (See make help for a complete list):
Run the unit tests: make test or make watch for continuously running tests on code-changes.
Build the documentation: make docs
TODO: Update the poetry.lock file: make lock
Add a dependency:
Start a shell in a new container.
Add dependency with poetry add in the running container. This will update poetry.lock automatically:
# 1. On the host system % make shell # 2. In the container instance: I have no name!@7d0e85b3a303:/app$ poetry add --dev --lock falcon
Build the development image make devimage (Note: This should be done automatically for the targets.)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for pandas_selector-0.1.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | be8f26ecd242b890ca83212b8b461e6a29c8628b691498c3df695a3297501d53 |
|
MD5 | 15a84edbbdfa2a93f2b4c83d3e38fe6a |
|
BLAKE2b-256 | 727b45fc63f527b131f8e6100892ddfb7c8ef4c36e7a41f803b5a9d79f9be86f |