When we make an arbitrary invertible coordinate transformation $$s_i=s_i(q_1,q_2,...q_n,t),\forall i,$$ the Lagrange's equation in terms of old coordinates $$\frac{d}{dt}\left(\frac{\partial L}{\partial\dot{q}_i}\right)-\frac{\partial L}{\partial q_i}=0,\forall i$$ changes to $$\frac{d}{dt}\left(\frac{\partial \hat{L}}{\partial\dot{s}_i}\right)-\frac{\partial \hat{L}}{\partial s_i}=0, \forall i$$ where $\hat{L}(s,\dot{s},t)$ is obtained from $L(q,\dot{q},t)$ by $$\hat{L}(s,\dot{s},t)=L(q(s,t),\dot{q}(s,\dot{s},t),t).$$
When we go to the Hamiltonian framework, things do not remain so simple. Under an arbitrary transformation in phase space, $$~~~Q_i\to Q_i(q_1,q_2...,p_1,p_2,..,t),\forall i,\\ P_i\to P_i(q_1,q_2...,p_1,p_2,..,t), \forall i$$ the Hamilton's equations do not remain form invariant. This only happens for a restricted class of transformations, called the canonical transformations. Also the new Hamiltonian is not obtained from the old Hamiltonina by $$\hat{H}(Q,P,t)= H(q_i(Q,P,t),p_i(Q,P,t),t)$$ even when the tranformation is canonical, unless that canonical transformation is also a symmetry.
Why is the reason for this?