Excel API
Package fabrictools.excel — fonctions nommées comme en Excel.
Excel formula-style API for Spark DataFrames (Excel.XLookup, Excel.SumIf, …).
Use with fabrictools.read_lakehouse() and prepared tables from Power Query scripts
instead of Excel structured tables and worksheet formulas.
Namespaces exported:
Excel—Excel.XLookup,Excel.SumIf,Excel.SumIfs,Excel.If,Excel.Round,Excel.TextJoin
Example
>>> from fabrictools import read_lakehouse, Excel
>>> projects = read_lakehouse("Lakehouse", "dbo/backlog_projects")
>>> df = Excel.XLookup(projects, "RAO CODE", customer_projects, "RAO CODE", "Date")
- class fabrictools.excel.Excel[source]
Bases:
objectNamespace for Excel worksheet functions on Spark DataFrames.
Names match Excel EN function names (
XLookup=XLOOKUP/RECHERCHEX).- static If(condition: pyspark.sql.Column, value_if_true: pyspark.sql.Column | str | int | float | bool | None, value_if_false: pyspark.sql.Column | str | int | float | bool | None) pyspark.sql.Column
Conditional expression (Excel
IF/SI).- Parameters:
- Returns:
Conditional column expression.
- Return type:
Column
Example
>>> from fabrictools import Excel >>> from pyspark.sql import functions as F >>> Excel.If(F.col("amount") > 0, "OPEN", "CLOSED")
- static Round(expr: pyspark.sql.Column | str, num_digits: int = 0) pyspark.sql.Column
Round a numeric column (Excel
ROUND/ARRONDI).- Parameters:
- Returns:
Rounded column expression.
- Return type:
Column
Example
>>> from fabrictools import Excel >>> Excel.Round(F.col("TO still to recognize"), 0)
- static SumIf(left: pyspark.sql.DataFrame, criteria_col: str, right: pyspark.sql.DataFrame, match_col: str, sum_col: str, *, output_name: str | None = None) pyspark.sql.DataFrame
Conditional sum keyed on one column (Excel
SUMIF/SOMME.SI).Pre-aggregates
rightwithgroupBy(match_col).sum(sum_col)then left-joins toleft[criteria_col]. This matches Excel row-wiseSUMIFsemantics without row loops.- Parameters:
left (DataFrame) – Driving dataframe.
criteria_col (str) – Column on
leftused as the criteria value.right (DataFrame) – Source table to aggregate.
match_col (str) – Column on
rightcompared tocriteria_col.sum_col (str) – Column on
rightto sum.output_name (str | None) – Name of the added column (defaults to
sum_col).
- Returns:
leftwith the conditional sum column appended.- Return type:
DataFrame
- Raises:
ValueError – If a required column does not resolve.
Example
>>> from fabrictools import Excel >>> df = Excel.SumIf(df, "RAO CODE", invoicing, "Project Number", "Total Invoice Amount", ... output_name="Total Invoicing")
- static SumIfs(left: pyspark.sql.DataFrame, sum_col: str, right: pyspark.sql.DataFrame, criteria: Mapping[str, str | int | float | bool | None], *, output_name: str | None = None) pyspark.sql.DataFrame
Multi-criteria conditional sum (Excel
SUMIFS/SOMME.SI.ENS).Each entry in
criteriamaps a right column to either:a left column name (joined after aggregation), or
a literal value (filters
rightbefore aggregation).
- Parameters:
left (DataFrame) – Driving dataframe.
sum_col (str) – Column on
rightto sum.right (DataFrame) – Source table to aggregate.
criteria (collections.abc.Mapping[str, str | int | float | bool | None]) –
{right_col: left_col_name | literal}mapping.output_name (str | None) – Name of the added column (defaults to
sum_col).
- Returns:
leftwith the conditional sum column appended.- Return type:
DataFrame
- Raises:
ValueError – If
sum_color join criteria do not resolve.
Example
>>> from fabrictools import Excel >>> df = Excel.SumIfs(df, "Turnover", turnover_follow, ... {"Project No.": "RAO CODE", "Year": 2022}, ... output_name="Turnover Follow 2022")
- static TextJoin(delimiter: str, ignore_empty: bool, *parts: pyspark.sql.Column | str) pyspark.sql.Column
Join text parts with a delimiter (Excel
TEXTJOIN/JOINDRE.TEXTE).- Parameters:
- Returns:
Concatenated column expression.
- Return type:
Column
Example
>>> from fabrictools import Excel >>> Excel.TextJoin(" | ", True, F.lit("a"), F.lit(""))
- static XLookup(left: pyspark.sql.DataFrame, lookup_col: str, right: pyspark.sql.DataFrame, match_col: str, return_col: str, *, default: Any = None, output_name: str | None = None) pyspark.sql.DataFrame
Lookup a value from
rightand add it toleft(ExcelXLOOKUP/RECHERCHEX).Performs a left join on
lookup_col(left) andmatch_col(right), keeping the first match whenrightcontains duplicate keys (Excel behaviour).- Parameters:
left (DataFrame) – Driving dataframe (equivalent to the row with
[@[lookup_col]]).lookup_col (str) – Column on
leftholding the lookup key.right (DataFrame) – Lookup table.
match_col (str) – Column on
rightto match against.return_col (str) – Column on
rightwhose value is returned.default (Any) – Value used when no match is found (
Nonekeeps null).output_name (str | None) – Name of the added column (defaults to
return_col).
- Returns:
leftwith the looked-up column appended.- Return type:
DataFrame
- Raises:
ValueError – If a required column does not resolve.
Example
>>> from fabrictools import Excel >>> df = Excel.XLookup(projects, "RAO CODE", customer_projects, "RAO CODE", "Date")