Srimad Bhagavatam Canto 04, Chapter 28, Text 28

SB 4.28.28

tam eva manasa grhnan
 babhuva pramadottama
anantaram vidarbhasya
 raja-simhasya vesmani
Translation by His Divine Grace A. C. Bhaktivedanta Swami Srila Prabhupada: 
King Purañjana gave up his body while remembering his wife, and consequently in his next life he became a very beautiful and well-situated woman. He took his next birth as the daughter of King Vidarbha in the very house of the King.
Purport by His Divine Grace A. C. Bhaktivedanta Swami Srila Prabhupada: 
Since King Purañjana thought of his wife at the time of death, he attained the body of a woman in his next birth. This verifies the following verse in Bhagavad-gita (8.6):
yam yam vapi smaran bhavam
 tyajaty ante kalevaram
tam tam evaiti kaunteya
 sada tad-bhava-bhavitah
“Whatever state of being one remembers when he quits his body, that state he will attain without fail.”
When a living entity is accustomed to think of a particular subject matter or become absorbed in a certain type of thought, he will think of that subject at the time of death. At the time of death, one will think of the subject that has occupied his life while he was awake, lightly sleeping or dreaming, or while he was deeply sleeping. After falling from the association of the Supreme Lord, the living entity thus transmigrates from one bodily form to another according to nature’s course, until he finally attains the human form. If he is absorbed in material thoughts and ignorant of spiritual life, and if he does not take shelter under the lotus feet of the Supreme Personality of Godhead, Govinda, who solves all questions of birth and death, he will become a woman in the next life, especially if he thinks of his wife. As stated in Srimad-Bhagavatam (3.31.1), karmana daiva-netrena. A living entity acts piously and impiously, and sometimes in both ways. All actions are taken into account, and the living entity is offered a new body by his superiors. Although King Purañjana was overly attached to his wife, he nonetheless performed many pious fruitive activities. Consequently, although he took the form of a woman, he was given a chance to be the daughter of a powerful king. As confirmed in Bhagavad-gita (6.41):
prapya punya-krtam lokan
 usitva sasvatih samah
sucinam srimatam gehe
 yoga-bhrasto ’bhijayate
“The unsuccessful yogi, after many, many years of enjoyment on the planets of the pious living entities, is born into a family of righteous people or into a family of rich aristocracy.”
If a person falls from the path of bhakti-yoga, God realization, due to attachment to fruitive activity, philosophical speculation or mystic yoga, he is given a chance to take birth in a high and rich family. The higher authorities appointed by the Supreme Personality of Godhead thus render justice to the living entity according to the living entity’s desires. Although King Purañjana was overly absorbed in thoughts of his wife and thus became a woman, he took birth in the family of a king due to his previous pious activities. The conclusion is that all our activities are taken into consideration before we are awarded another body. Narada Muni therefore advised Vyasadeva that one should take to Krsna consciousness, devotional service, and abandon all ordinary occupational duties. This advice was also given by Lord Krsna Himself. Although a devotee may fall from the path of spiritual consciousness, he will nonetheless attain a human body in the home of a devotee or a rich man. In this way one can resume his devotional service.
Srimad Bhagavatam Canto 04, Chapter 28, Text 27
Srimad Bhagavatam Canto 04, Chapter 28, Text 29