I assume that each block has at least two records, and also, if it has more than two, you want them to be assigned as close as possible to 80/20. The easiest way to do this is to assign a random number to all rows, and then choose based on percentiles in each stratified sample. Let's say this is the data in strat_sample.csv:
Index_1,Index_2,Data_1,Data_2 0,0,0.614583182,0.677644482 0,0,0.321384981,0.598450854 0,0,0.303029607,0.300593782 0,0,0.646010758,0.612006715 0,0,0.484572883,0.30052535 0,1,0.010625416,0.118671475 0,1,0.428967984,0.23795173 0,1,0.523440618,0.457275922 0,1,0.379612652,0.337640868 0,1,0.338180659,0.206399031 1,0,0.079386,0.890939911 1,0,0.572864624,0.725615079 1,0,0.045891404,0.300128917 1,0,0.578792198,0.100698871 1,0,0.776485138,0.475135948 1,0,0.401850419,0.784835723 1,1,0.087660923,0.497299605 1,1,0.8460978,0.825774802 1,1,0.526015021,0.581905971 1,1,0.23324672,0.299475291
Then this code (using Pandas data structures) works as desired
import numpy as np import random as rnd import pandas as pd