We experimentally investigate the effect of population viscosity (an increased probability to interact with others of one's type or group) on cooperation in a standard prisoner's dilemma environment. Subjects can repeatedly choose between two groups that differ in the defector gain in the associated prisoner's dilemma. Choosing into the group with the smaller defector-gain can signal one's willingness to cooperate. We find that viscosity produces an endogenous sorting of cooperators and defectors and persistently high rates of cooperation. Higher viscosity leads to a sharp increase in overall cooperation rates and in addition positively affects the subjects' preferences for cooperation.