data = pd.DataFrame({'cols1': [4, 5, 5, 4, 321, 32, 5], 'clol2': [45, 66, 6,
    6, 1, 432, 3], 'class': ['A', 'B', 'C', 'C', 'A', 'B', 'B']})
freq = pd.DataFrame({'class': ['A', 'B', 'C'], 'nostoextract': [2, 2, 2]})

def bootstrap(data, freq):
    freq = freq.set_index('class')

def sampleClass(classgroup):
    cls = classgroup['class'].iloc[0]
nDesired = freq.nostoextract[cls]
nRows = len(classgroup)
nSamples = min(nRows, nDesired)
return classgroup.sample(nSamples)
samples = data.groupby('class').apply(sampleClass)
samples.index = samples.index.get_level_values(1)
return samples
print(bootstrap(data, freq))
Sampling a dataframe based on a given distribution