How to apply stratified sampling on a dataset?

I have a picture of the output in my course notebook but no accompanying code is provided. How can I implement the logic in the code to get the same output? My teacher only demonstrated the output without showing the actual code. She created a dummy dataset with 4 values of ‘spam’ and 4 values of ‘not spam’.