superleaf.dataframe.selection#
Functions
|
Select the rows of a DataFrame that pass every filter. |
|
Split a DataFrame into rows that pass every filter and rows that do not. |
|
Move columns to the front, the back, or next to another column. |
Classes
|
Row filter evaluated on the whole DataFrame at once. |
- class superleaf.dataframe.selection.where(f: Callable[[DataFrame], Any])[source]#
Bases:
objectRow filter evaluated on the whole DataFrame at once.
Use with
dfilter()orpartition()for vectorized conditions across columns. Apd.colexpression can be passed directly instead.- Parameters:
f (callable) – Function from the DataFrame to a boolean Series or array with one value per row.
Examples
>>> dfilter(df, where(lambda d: d["a"] > d["b"]))
- superleaf.dataframe.selection.dfilter(df: DataFrame, *filters, **col_filters) DataFrame[source]#
Select the rows of a DataFrame that pass every filter.
- Parameters:
df (pd.DataFrame) – The DataFrame to filter. A Series is also accepted; its index can be filtered with
index=....*filters –
Row filters, each one of:
a
pd.colexpression, such aspd.col("a") > pd.col("b");a
where, evaluated once on the whole DataFrame;another callable, applied to each row (a Series) and returning a bool;
a dict of column filters, as for
**col_filters(for column names that are not identifiers);a boolean sequence or Series with one value per row.
**col_filters –
Column filters, mapping a column name to one of:
a
BooleanOperatorsuch asF.gt(0), evaluated withon_series;another callable, applied to each value;
a scalar, selecting values equal to it (as
F.eq).
Lists and other non-scalar values raise
TypeError; useF.isinfor membership.The keyword
columns, unless it names a column ofdf, selects columns instead of rows. It takes a column name, a predicate on names, or a list of either; columns matching any of them are kept.
- Returns:
The selected rows (and columns), as a new DataFrame.
- Return type:
pd.DataFrame
- Raises:
TypeError – If a filter has an unsupported type.
ValueError – If a boolean filter’s length or index does not match
df.
Examples
>>> dfilter(df, status="active", age=F.ge(30)) >>> dfilter(df, name=F.startswith("A") | F.isna, columns=["name", "age"]) >>> dfilter(df, pd.col("score") > pd.col("threshold"), status="active")
- superleaf.dataframe.selection.partition(df: DataFrame, *filters, **col_filters) tuple[DataFrame, DataFrame][source]#
Split a DataFrame into rows that pass every filter and rows that do not.
- Parameters:
- Returns:
pd.DataFrame – The rows that pass.
pd.DataFrame – The remaining rows.
Examples
>>> passed, failed = partition(df, score=F.gt(50))
- superleaf.dataframe.selection.reorder_columns(df: DataFrame, columns: str | Sequence[str], back=False, after=None, before=None, errors='raise') DataFrame[source]#
Move columns to the front, the back, or next to another column.
- Parameters:
df (pd.DataFrame) – The DataFrame whose columns to reorder.
columns (str or sequence of str) – Column(s) to move, in the order they should appear.
back (bool, optional) – If True, move the columns to the end.
after (str, optional) – Place the columns immediately after this column.
before (str, optional) – Place the columns immediately before this column.
errors ({"raise", "warn", "ignore"}, optional) – How to handle names in
columnsthat are not indf. With"warn"or"ignore", they are skipped.
- Returns:
A new DataFrame with reordered columns. With none of
back,afterorbefore, the columns are moved to the front.- Return type:
pd.DataFrame
- Raises:
ValueError – If more than one of
back,afterandbeforeis given, orerrorsis invalid.KeyError – If a column is missing and
errors="raise".
Examples
>>> reorder_columns(df, ["age", "name"], after="id")