bin DataFrame in pandas for heatmap

GeauxEric

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.

Alexander

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

seaborn heatmap using pandas dataframe

From Dev

Annotate heatmap with value from Pandas dataframe

From Dev

How to plot a heatmap from pandas DataFrame

From Dev

Type error in visualising pandas dataframe as heatmap

From Dev

Plotting 3 cols of pandas dataframe as heatmap

From Dev

Pivoting a pandas dataframe to generate a (seaborn) heatmap

From Dev

Convert a Pandas DataFrame to bin frequencies

From Dev

Bin pandas dataframe by every X rows

From Dev

pandas and seaborn - heatmap with no colors

From Dev

Pandas Dataframe - Bin on multiple columns & get statistics on another column

From Dev

Plotting a heatmap with different bin sizes in Gnuplot

From Dev

Make heatmap of presence of levels for a dataframe

From Dev

Create heatmap using pandas TimeSeries

From Dev

Create heatmap using pandas TimeSeries

From Dev

generate a heatmap from a dataframe with python and seaborn

From Dev

Trying to create heatmap from DataFrame, or Matrix

From Dev

How to create a histogram where each bar is a heatmap of the corresponding bin?

From Dev

Pandas groupby with bin counts

From Dev

Pandas bin and count

From Java

Pandas Dataframe

From Dev

Pandas Dataframe

From Dev

Bin values based on ranges with pandas

From Dev

Bin datetime range using pandas

From Dev

How to bin groupeby counts in pandas?

From Dev

How to Dynamically bin data in Pandas

From Dev

converting unstacked dataframe into a dataframe in pandas

From Dev

transform a pandas dataframe in a pandas with multicolumns

From Dev

Pandas assign each row the mean of its bin

From Dev

pandas dataframe by index and integer