The local particle density is a function of the point. Therefore, to get it from some derivative, one needs to introduce a derivative with respect to a function of the point.
This is something already done by mathematicians, and it is a functional derivative. If one does not know about functional derivatives, it would be too long to provide a minimum of the underlying theory here. However, when we are equipped with functional derivatives, it is relatively simple to show that if we add to a many-body Hamiltonian the interaction with an external one-particle field $u(\bf{r})$, the one-particle density $n(\bf{r})$ can be obtained from a generalization of the formula for the average number:
$$
n({\bf r}) = \frac{\delta \ln Z }{\delta \ln z^*({\bf r}) },
$$
where $Z$ is the grand canonical partition function as a functional of
$$
z^*({\bf r}) = \frac{e^{\beta (\mu - u(\bf{r}))}}{\Lambda^3},
$$
where $\beta = \frac{1}{k_B T}$, $\mu$ is the chemical potential, and $\Lambda$ the de Broglie thermal length. All details can be found wherever there is an introduction to classical density functional theory.