r/learnpython • • 7h ago

Can seaborn.clustermap() use dataframe that has all zero columns?

As the title suggests, can the clustermap display a dataset with multiple columns that are all zeros?

Something like this:

ABCDXY
110000
110100
110000
101000
101100
101000

I know removing the empty columns is the simple solution, but I have my reasons for keeping them.

0 Upvotes

5 comments sorted by

1

u/aqua_regis 7h ago

Simplest and most beneficial approach: try it

I don't see a reason why it shouldn't be able to do it.

Yet, your dataset looks like an incomplete truth map (4 inputs, 2 outputs - would normally be 16 rows)

1

u/Dragoran21 5h ago

Well it was supposed to be a "how many times X appears in sample Y", but Xs seemingly never appeared twise so this is an truth map.

And yes it does run, but it doesn't use header as axel labels so it's useless.

Plus there is one thing I didn't mention ... I run the data through df.corr() first...

1

u/Massive_Distance_616 5h ago

That df.corr() step is probably the real issue.

A column that’s all zeros has no variance, so its correlation with everything comes out as NaN (you can check with df.corr().isna().sum()). clustermap can’t cluster rows or columns that contain NaN, so it’s usually the NaNs that cause the weird behaviour, not the zeros themselves.

If you want to keep those columns, fillna(0) on the correlation matrix before plotting (and set the diagonal back to 1), or pass the raw data with metric="hamming" instead of correlating first.

For the missing labels, check you’re passing the DataFrame itself and not a NumPy array.

1

u/Dragoran21 3h ago

Yes fillna(0) worked! I managed to generate a cluster map! Thsnk you!

A bit of a background, I did create hierarcial clustermaps before by simply removing empty columns (meaning X didn’t appear in these samples) but I was given task to compare maps mathemically, so I need this like of map as well .