# Getting a balanced sample across many variables

Let’s say each element in my population has several attributes. Let’s call then A, B, C, D, E, F.

Let’s say, for simplicity, each attribute has 10 values (but could be any number between 2 and 30). Now I want to get a sample such that the distribution is the same across all features. So for example if the whole population has about 15% of people in feature A with value 1, my sample should be the same.

What should be the way for me to select a size for the sample and choose a sample that has the desired properties?

• Do you assume that attribute distributions are normal or not ? Aug 4 '20 at 15:46
• No, they will not be
– user
Aug 4 '20 at 16:03

If I understand correctly, you want a sample with along each feature the value below which a given percentage of observations in your population falls. If so, you might want to try something like this using np.percentile:

import numpy as np
import pandas as pd

data = [ np.random.randint(1,10,6) for i in range(20) ] # fake data
df = pd.DataFrame(data=data, columns=['A', 'B', 'C', 'D', 'E', 'F']) # fake dataframe

# 15 being your desired percentage
balanced_sample = [ np.percentile(df[col], 15) for col in df.columns ]

• Maybe I don’t understand, but this won’t do what I want. I want the sample to have the same percentage for every column and every value of each column at the same time
– user
Aug 4 '20 at 17:58
• By sample, you mean one sample from the population right ? (Maybe this is what I got wrong...) If so, you want to create a sample that has the same percentage for every columns (my example does that) and with the same value for every column as well ? (my example does not) Aug 5 '20 at 7:03