# Create files for a list of categories
# a file for Tools.xlsx, PPE.xlsx and Electrical.xlsx will be created
write:
- matrix:
variables:
category1: ["Tools", "PPE", "Electrical"] # Variables as a List of Values, uses list items as categories
category2: set(Category) # Variables as a Set (From Column Data), gets all unique values from the Category column
category3: custom.get_list() # Variables From a Custom Function, runs custom function that returns a list
write:
- file:
name: ${category}.xlsx
where: category = ?
where_params:
- ${category}
The above example shows 3 different ways to implement variables: List of Values, Variables as a Set, and Variables From a Custom Function.
def get_list():
return ["Tools","PPE","Electrical"]
Product |
Price |
Quantity |
Category |
Hammer |
$3.99 |
31 |
Tools |
Safety Glasses |
$1.99 |
12 |
PPE |
Hard Hat |
$12.99 |
4 |
PPE |
Extension Cord |
$4.95 |
14 |
Electrical |
Screw Driver |
$11.99 |
17 |
Tools |
Wire Nuts |
$1.99 |
11 |
Electrical |
Product |
Price |
Quantity |
Category |
Hammer |
$3.99 |
31 |
Tools |
Screw Driver |
$11.99 |
17 |
Tools |
Product |
Price |
Quantity |
Category |
Safety Glasses |
$1.99 |
12 |
PPE |
Hard Hat |
$12.99 |
4 |
PPE |
Product |
Price |
Quantity |
Category |
Extension Cord |
$4.95 |
14 |
Electrical |
Wire Nuts |
$1.99 |
11 |
Electrical |
import pandas as pd
from wrangles.connectors import matrix
products_dataframe = pd.read_excel('product_file.xlsx')
matrix.write(df = products_dataframe, variables={'category': ['Tools', 'PPE', 'Electrical']}, write=['file':{'name': '${category}.xlsx', 'where': 'category = ?', 'where_params':['${category}']}])
Parameter |
Required |
Data Type |
Notes |
df |
✓ |
Pandas Dataframe |
The input dataframe |
variables |
✓ |
dict |
A list of variables. The write will be executed once for each combination of variables. Can be passed through as a list, a column based set, or with a custom function that returns a list. |
write |
✓ |
list |
The write section of a recipe to execute for each combination of variables. |
functions |
|
list |
Custom functions to provide to the write recipes. |
strategy |
|
string |
Determines how to combine variables when there are multiple. loop (default) iterates over each set of variables, repeating shorter lists until the longest is completed. permutations uses the combination of all variables against all other variables. |
order_by |
|
str |
Uses SQL syntax to sort the output. |