ComfyUI Node

Pandas Xs

Selecting Rows From a MultiIndex DataFrame, Sans Panic

By HowToSD·Created 2 years ago·Updated about a year ago· 23
Pandas Xs
  • dataframe
  • DATAFRAME
row_or_column_label
label_type
level
level_type
axis

Most tables in this pack are flat - columns and rows, nothing fancy. But the whole point of the Data Analysis pack is things like the author's baseball example: data with multiple index levels, like (Year, Player Name). Grabbing one specific slice of that - "all of 2019, or one player across years" - is what Pandas Xs is for. It wraps pandas' xs() method, which is the proper way to pull a subset out of a MultiIndex.

If you've never used a MultiIndex, skip this node until you need it. When you do need it, you'll be glad it's here, because hand-writing df.xs() into a workflow that's otherwise all nodes is the worst kind of hybrid.

How it works

It's a direct wrapper around df.sort_index().xs(label, level=..., axis=...). You give it a label (or comma-separated labels for multiple index levels), tell it which level(s) of the MultiIndex to search, and whether those labels live on the row index or the columns. It sorts the frame first, then returns the matching subset as a new DataFrame.

The level field is the bit everyone finds confusing. In a MultiIndex, "level" just means "which position in the stack of index labels" - level 0 is the outermost, level 1 the next, and so on. If your index is (Year, Player) and you want everything for one player, you'd search the label at level 1. Multiple labels and their levels go in comma-separated: label dog, brown at levels 1, 2.

Inputs and outputs

The inputs that matter:

  • row_or_column_label - the label(s) to select, comma-separated for multiple.
  • label_type - string or int, matching the actual dtype of the labels.
  • level - which MultiIndex level(s) to search, comma-separated; blank means the default level.
  • level_type - int or string, matching the level value.
  • axis - index (rows) or columns (column labels).

One output: a DATAFRAME with the selected rows or columns, ready to feed a plot or a show node.

Install

Part of HowToSD/ComfyUI-Data-Analysis. Via Manager: search "Data analysis" in the Custom Node Manager, install, restart, reload the browser tab. Manual:

cd ComfyUI/custom_nodes
git clone https://github.com/HowToSD/ComfyUI-Data-Analysis.git
mv ComfyUI-Data-Analysis data-analysis   # README: example workflows expect this folder name
pip install -r requirements.txt

No model downloads, no GPU required. The rename step is the one the README explicitly warns about.

Common issues

Type mismatches are the failure mode here. If you tell it the label is an int but the index actually holds strings, pandas raises - the node passes your choice straight through. Same for level_type. If you're getting errors, check the actual dtype of your index first (a Show Index node from this pack helps). Also, single-label selection returns a DataFrame but multi-label selection builds a tuple key - the node handles that conversion for you, which is exactly the fiddly bit you don't want to get wrong by hand. If your labels are hierarchical but the index is flat, this node isn't the right tool - look at Pandas Select Rows instead.

CategoryData Analysis

Inputs (6)

NameTypeDefaultDescription
dataframeDATAFRAME
row_or_column_labelSTRING
label_typeCOMBO2 options: string, int
levelSTRING
level_typeCOMBO2 options: int, string
axisCOMBO2 options: index, columns

Outputs (1)

NameTypeDescription
DATAFRAMEDATAFRAME