Supercharged DataFrame indexing
Project description
pandas-select is a collection of DataFrame selectors that facilitates indexing and selecting data, fully compatible with pandas vanilla indexing.
The selector functions can choose variables based on their name, data type, arbitrary conditions, or any combination of these.
pandas-select is inspired by two R libraries: tidyselect and recipe.
Installation
pandas-select is a Python-only package hosted on PyPI. The recommended installation method is pip-installing into a virtualenv:
$ pip install pandas-select
Design goals
Fully compatible with pandas.DataFrame [] and pandas.DataFrame.loc accessors.
Emphasise readability and conciseness by cutting boilerplate:
# pandas-select
df[AllNumeric()]
df[StartsWith("Type") | "Legendary"]
# vanilla
cols = df.select_dtypes(exclude="number").columns
df[cols]
cond = lambda col : col.startswith("Type") or col == "Legendary"
cols = [col for col in df.columns if cond(col)]
df[cols]
Ease the challenges of indexing with hierarchical index and offers an alternative to slicers when the labels cannot be listed manually.
# pandas-select
name = Contains("Jeff", axis="index", level="Name")
df_mi.loc[selector]
# vanilla
selector = df_mi.index.get_level_values("Name").str.contains("Jeff")
df_mi.loc[selector]
Allow deferred selection when the DataFrame’s columns are not known in advance, for example in automated machine learning applications. pandas_select offers integration with sklearn.
from pandas_select import AnyOf, AllBool, AllNominal, AllNumeric, ColumnSelector
from sklearn.compose import make_column_transformer
from sklearn.preprocessing import OneHotEncoder, StandardScaler
ct = make_column_transformer(
(StandardScaler(), ColumnSelector(AllNumeric() & ~AnyOf("Generation"))),
(OneHotEncoder(), ColumnSelector(AllNominal() | AllBool() | "Generation"))
)
ct.fit_transform(df)
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
Built Distribution
Hashes for pandas_select-0.1.5-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a2212795575b3fd4a84afe3ed9dc7c051a7c7b92c04d958d24ec56e2cb939245 |
|
MD5 | 2e8402e2de722033be0f54d0827facc9 |
|
BLAKE2b-256 | 3424b14387db7af3c7f5a197569a23b21d479547a989ca9f73f64a0cf62e721c |