ComfyUI Node

Pandas Sub

Subtract one DataFrame from another, cell by cell

By HowToSD·Created 2 years ago·Updated about a year ago· 23
Pandas Sub
  • a_dataframe
  • b_dataframe
  • DATAFRAME

Pandas Sub subtracts one DataFrame from another, element by element - a minus b, cell-for-cell. It's the DataFrame-level member of the pack's arithmetic family (there are scalar and Series variants too: Pandas Sub Scalar Int, Pandas Sub Scalar Float, Pandas Sub Series), and it's the one you reach for when both sides of the subtraction are real tables rather than a single number.

Concrete use: you loaded last month's metrics and this month's metrics as two frames, and you want the per-cell change - the delta frame you can plot or sum. Or you loaded a "predicted" frame and a "measured" frame and want the raw residual before you summarize it. It's a diff operation with structure, which is why pandas alignment matters here more than anywhere.

How it works

The implementation is a_dataframe.sub(b_dataframe). "Sub" in pandas isn't just arithmetic - it's aligned arithmetic: the two frames are matched up by row labels and column labels before subtraction. If a label exists in only one frame, the result at that position is NaN. That's pandas behaving correctly, but it's also the thing that produces walls of NaN if your two frames weren't built with the same index and columns.

Inputs and outputs

  • a_dataframe (DATAFRAME) - the left (minuend) frame, the one being subtracted from.
  • b_dataframe (DATAFRAME) - the right (subtrahend) frame.

Output: a single DATAFRAME - the aligned difference. Wire it into a Pandas Summary to see if the deltas are meaningful, a plotting node to visualize the change, or Pandas To Numpy if you're heading into the numpy side.

Installing this pack

This node ships in ComfyUI-Data-Analysis by Hide Inada (HowToSD). CPU-only, no GPU, no model downloads - but the pack needs its Python stack, which stock ComfyUI lacks: pandas, matplotlib, seaborn, scipy, scikit-learn, openpyxl, lxml.

ComfyUI Manager: Manager → Custom Node Manager → search "Data analysis" → install ComfyUI-Data-Analysis → restart ComfyUI and refresh the browser. Manager installs the deps.

Git clone:

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

Restart ComfyUI after installing.

Troubleshooting

  • My result is full of NaN. Your two frames don't share the same row/column labels - pandas aligned them and found nothing to subtract. Give both frames the same index (a common key column, set via Pandas Set Index) before subtracting.
  • Shapes don't match. Aligned subtraction doesn't require equal shapes, which is exactly why you can get a bigger-than-expected result with padding. Check what labels actually exist in each frame with Pandas Columns and Pandas Index.
  • It only works on the same dtype. Subtracting a string column from a string column raises; keep this node for numeric frames.
CategoryData Analysis

Inputs (2)

NameTypeDefaultDescription
a_dataframeDATAFRAME
b_dataframeDATAFRAME

Outputs (1)

NameTypeDescription
DATAFRAMEDATAFRAME