Let $G=SL_2(\mathbb R)=NAK$ with $N,A,K$ as in Iwasawa decomposition and $f:SL_2(\mathbb R)\to \mathbb R$. Show that for $h:N\to\mathbb R$ defined by $h\begin{pmatrix}1&u\\0&1\end{pmatrix}=(1+u^2)^{-1}$ for which $$\int_K f(gk)dk=\frac1\pi\int_Nf(gn)h(n)dn.$$
That allegedly has to follow by writing $k=\begin{pmatrix}\cos\theta&\sin\theta\\-\sin\theta&\cos\theta\end{pmatrix}$ and applying change of variable $\theta=\arctan u$ (then identifying $\mathbb R\ni u\mapsto \begin{pmatrix}1&u\\0&1\end{pmatrix}\in N$) but that doesn't explain the constant factor of $\frac 1\pi$.
Where this factor comes from? Is there some "purely algebraic way" to show this claim?
This is not a proof but some light shed on your integral formula. There is indeed not a "purely algebraic", but a measure theory (probabilistic would even be a better term) way to see it, justifying the presence of a $\dfrac{1}{\pi}$ factor.
Let us write your formula here for further reference:
$$\tag{1}\int_K f(gk)dk=\int_Nf(gn)\frac{1}{\pi}h(n)dn.$$
(note that we have incorporated the $\frac{1}{\pi}$ factor inside the integral.)
The 2 following functions are related:
is the pdf (probability density function) of the so-called Cauchy distribution.
(recall that $atan$ is used in the change of variable) is the cdf (cumulative distribution function) associated with (2).
Note the presence of $\dfrac{1}{\pi}$ factor as well in (3).
Thus the RHS of (1) can be interpreted as a weighing by Cauchy density.