I would like to plot a heatmap from a DataFrame in pandas. The data looks like
df = pd.DataFrame({"A": np.random.random(100), "B": np.random.random(100), "C": np.random.random(100)})
To show how C changes as a function of A and B, I want to bin the data based on A and B and calculate the average value of C in each bin, finally the heatmap has A and B as X-axis and Y-axis, and the color indicates the corresponding C value.
I tried to use Seaborn.heatmap, but the function accepts square dataset, which means I should bin the data first.
Is there a way to directly generate what I want from the DataFrame? If not, how can I bin DataFrame into 2-D grids?
I know pandas.cut can do the trick, but it seems only be able to cut based on one column one time. Of corse I can write tedious function to pipeline 'two cuts', but I am wondering if there is some simple way to do the task.
Scatter plot can give similar results but I want heatmap, something like this, not this.
Something like this?
>>> df.groupby([pd.cut(df.A, 3), pd.cut(df.B, 3)]).C.mean().unstack()
B (0.00223, 0.335] (0.335, 0.666] (0.666, 0.998]
A
(0.000763, 0.334] 0.579832 0.454004 0.349740
(0.334, 0.667] 0.587145 0.677880 0.559560
(0.667, 1] 0.566409 0.496061 0.420541
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments