How to present a sparse matrix of differences between two Python dataframe objects?

For auditing purposes, we want to compare two objects of Python dataframe of the pandas library in JupyterLab Notebook, and they contain the before (Exist) and later (Stage) versions of the same data set.

If we call the .compare() function like the sample source code below, we will get result formatted as a matrix with self and other sub-columns for each column in the dataframes in compare.

# compare dataframes
diff = df1.compare(df2)

# d1: Exist (self)
# d2: Stage (other)

If the element stays the same in the two dataframes, the result matrix contains NaN for both self and other in the corresponding position. And we believe Python treats NaN as null.

See the example below, in our case, the most the elements in the comparing result are NaN, so it is like a sparse matrix. And there is only a small portion of differences, but they exist in every row, on different columns, e.g., AAA, BBB, CCC, DDD in the example below.

...
dir	branch_class	status_indicator	ref	language_code	...	province	postal_code	service	exchange_point	cycle
self	other	self	other	self	other	self	other	self	other	...	self	other	self	other	self	other	self	other	self	other
0 Exist	Stage	NaN	NaN	NaN	NaN	NaN	NaN	NaN	NaN	...	NaN	NaN	AAA	BBB	NaN	NaN	NaN	NaN	xxxxxx	yyyyyy
1 Exist	Stage	NaN	NaN	NaN	NaN	NaN	NaN	NaN	NaN	...	NaN	NaN	NaN	NaN	NaN	NaN	NaN	NaN	xxxxxx	yyyyyy
2 Exist	Stage	NaN	NaN	NaN	NaN	NaN	NaN	NaN	NaN	...	NaN	NaN	CCC	DDD	NaN	NaN	NaN	NaN	xxxxxx	yyyyyy
3 Exist	Stage	NaN	NaN	NaN	NaN	NaN	NaN	NaN	NaN	...	NaN	NaN	NaN	NaN	NaN	NaN	NaN	NaN	xxxxxx	yyyyyy
4 Exist	Stage	NaN	NaN	NaN	NaN	NaN	NaN	NaN	NaN	...	NaN	NaN	NaN	NaN	NaN	NaN	NaN	NaN	xxxxxx	yyyyyy
...	...	...	...	...	...	...	...	...	...	...	...	...	...	...	...	...	...	...	...	...	...
285	Exist	Stage	NaN	NaN	NaN	NaN	NaN	NaN	NaN	NaN	...	NaN	NaN	NaN	NaN	NaN	NaN	NaN	NaN	xxxxxx	yyyyyy
286	Exist	Stage	NaN	NaN	NaN	NaN	NaN	NaN	NaN	NaN	...	NaN	NaN	NaN	NaN	NaN	NaN	NaN	NaN	xxxxxx	yyyyyy
287	Exist	Stage	NaN	NaN	NaN	NaN	NaN	NaN	NaN	NaN	...	NaN	NaN	NaN	NaN	NaN	NaN	NaN	NaN	xxxxxx	yyyyyy
288	Exist	Stage	NaN	NaN	NaN	NaN	NaN	NaN	NaN	NaN	...	NaN	NaN	NaN	NaN	NaN	NaN	NaN	NaN	xxxxxx	yyyyyy
289	Exist	Stage	NaN	NaN	NaN	NaN	NaN	NaN	NaN	NaN	...	NaN	NaN	NaN	NaN	NaN	NaN	NaN	NaN	xxxxxx	yyyyyy
290 rows × 32 columns

Our Question

As you have already seen, the comparing result matrix is hard to read by human eyes. So, we want to enhance the presentation, ideally:

  • For each row, we only display the columns with differences.
  • Our usage is a data set with 30 columns, 20’000 rows, and typically 300 updates in each compare.

We highly appreciate any hints and suggestions. And, please also let us know if we should move this topic to a better category.

Updates

  • We got a hint somewhere else referencing to Pandas: drop columns with all NaN’s. Unfortunately, it is not the solution. We have tried running diff.dropna(axis=1, how='all') but the operation does not remove any column. As we said in the post, for the columns that show all equal in the sample display, they have differences in the other rows outside the sample.

This sounds like a data visualisation problem. Matplotlib has a spy plot for sparse matrices
https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.spy.html

You can easily create a custom plot, but anything more advanced probably depends on the meaning of the data.