Skip to content
Advertisement

Python Pandas Loop through Dictionary Keys (which are tuples) and plot variables against each other

I have a correlation matrix (in the form of a DataFrame) from which I return a Series which is the top n correlated pairs of columns and the value of the correlation:

JavaScript

See this for an example of what I mean. I take the resulting Series object and then cast as a dictionary like so:

JavaScript

The resulting keys of this dictionary are tuples of the top n correlated variables, which I found by:

JavaScript

Resulting in 15 keys:

JavaScript

Now, what I would like to do is go back to my original DataFrame from where I calculated the correlations and plot these tuples of columns against one another looping through the dictionary keys.

Kind of like this:

Key1 = (‘HCT’, ‘HGB’)

JavaScript

Key2 = (‘ALT’, ‘AST’)

JavaScript

In a sense I want to “unpack” (don’t know if I am using that word right) these tuples and plot them against each other?

Is this possible, or am I just trippin’?

Thanks in avbance

Advertisement

Answer

You can iterate over dictionary keys in a for loop and then subset each element within the tuple:

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement