# How to organize the 2D data into binned distribution?

Consider a data

data = {{x1,y1},{x2,y2},...}


with n rows. I want it to be organized into $$n$$ bins for $$x$$ and $$m$$ bins for y, with the positions $$x_{i}, x_{i+1}$$ and $$y_{j}, y_{j+1}$$ of the bins chosen in the way such that the number of events in each bin will be the same. And the resulting data should be

dataBinned = {{(x1+x2)/2, (y1+y2}/2, N/((x2 - x1)(y2-y1))},{(x1+x2)/2, (y2+y3}/2, N/((x2 - x1)(y3-y2))}, …, {(x2+x3)/2, (y1+y2}/2, N/((x3 - x2)(y2-y1))},{(x2+x3)/2, (y2+y3}/2, N/((x3 - x2)(y3-y2))}, …}


Here, $$N$$ denotes the number of events in each bin (it should be provided "automatically" by the procedure which finds $$x_{i}, y_{j}$$).

The method which makes the same for one-dimensional data data1 (see also a question) is the following:

(*Organizing data into bins*)
data1Bins = Partition[Sort[#], Round[Length[#]/#2]] &[data1, n];
(*Construction the binned data*)
data1Binned =
Table[{(data1Bins[[i]][] + data1Bins[[i]][[Length[data1Bins[[i]]]]])/2 //. {x_} :> x, Length[data1Bins[[i]]], Length[data1Bins[[i]]]/(data1Bins[[i]][[
Length[data1Bins[[i]]]]] - data1Bins[[i]][]) //. {x_} :> x}, {i, 1, n, 1}];


But I do not imagine how to construct the same for 2-dimensional data. Could you please help me with this?

I don't think what you want is possible for all cases. As a simple example, consider the following data set {{x1,y1},{x2,y2},...} = {{1,1,},{2,2},...}. Let the gridlines represent the bins. There is no way to bin it out such that the off diagonal bins can have any content. 