Observations in the ASDEX Upgrade tokamak show a correlation between the gradient of the intrinsic toroidal rotation profile and the logarithmic gradient of the electron density profile. The intrinsic toroidal rotation in the center of the plasma reverses from co- to countercurrent when the logarithmic density gradients are large, and the turbulence is either dominated by trapped electron modes or is at the transition between ion temperature gradient and trapped electron modes. A study based on local gyrokinetic calculations suggests that the dominant trend in the observations can be explained by the combination of residual stresses produced by E × B and profile shearing mechanisms.