Systematic Sampling in Pandas

13 Sep, 2021
Sampling is the method where one can take subset (Sample) from the given data and will investigate on the sample without investigating each individual thing of data. For instance, suppose in a College, someone wants to check the average height of Students who are Studying in the college. One way is to Collect data of all student and will calculate that but this task is very time-consuming. Thus, sampling is used. So, the solution is that during the Recess, randomly choose Students from Canteen and measure their height and will calculate the average height from that Subset of Student’s.
Types Of Sampling :
Sampling
Systematic Sampling is defined as the type of Probability Sampling where a researcher can research on a targeted data from large set of data. Targeted data is chosen by selecting random starting point and from that after certain interval next element is chosen for sample. In this a small subset (sample) is extracted from large data.
Suppose that size of Data is D and N will be the Size of Sample that we want to Select. So according to Systematic Sampling :
Interval = (D/N)
Suppose (D/N) = J
So when we choose first random element E from Data , the next element for Sample would be (E+J)
Example : Total Size of Data  = 50 (1 to 50)
          We want elements in  Sample = 5
          Interval = 50/5 = 10 .
It means in a sample we want gapping of 10 elements Systematically.
Suppose i randomly choose element first Sample Element  = 5
So next would be 5+10 = 15
                 15+10= 25
                 25+ 10 =35
                 35+10  = 45
So, 
Sample = { 5,15,25,35,45 }
Diagrammatically, 
Approach:
Take Data.
Extract Systematic Sample from large Data.
Print the Average of Sample Data.
Program: 
Python3
# Import in order to use inbuilt functionsimport numpy as npimport pandas as pd # Define total number of studentsnumber_of_students = 15 # Create data dictionarydata = {'Id': np.arange(1, number_of_students+1).tolist(),        'height': [159, 171, 158, 162, 162, 177, 160, 175,                   168, 171, 178, 178, 173, 177, 164]} # Transform dictionary into a data framedf = pd.DataFrame(data) display(df) # Define systematic sampling functiondef systematic_sampling(df, step):     indexes = np.arange(0, len(df), step=step)    systematic_sample = df.iloc[indexes]    return systematic_sample  # Obtain a systematic sample and save it in a new variablesystematic_sample = systematic_sampling(df, 3) # View sampled data framedisplay(systematic_sample)
Output:
Example: Print Average of Sample Data
Python3
# Import in order to use inbuilt functionsimport numpy as npimport pandas as pd # Define total number of studentsnumber_of_students = 15 # Create data dictionarydata = {'Id': np.arange(1, number_of_students+1).tolist(),        'height': [159, 171, 158, 162, 162, 177, 160, 175,                   168, 171, 178, 178, 173, 177, 164]} # Transform dictionary into a data framedf = pd.DataFrame(data)  # Define systematic sampling functiondef systematic_sampling(df, step):     indexes = np.arange(0, len(df), step=step)    systematic_sample = df.iloc[indexes]    return systematic_sample  # Obtain a systematic sample and save it in a new variablesystematic_sample = systematic_sampling(df, 3) # View sampled data framedisplay(systematic_sample) # Empty Print Statement for new lineprint() # Save the sample data in a separate variablesystematic_data = round(systematic_sample['height'].mean())print("Average Height in cm: ", systematic_data)
Output:
Systematic Sampling is of three types as depicted below :
Types of Systematic Sampling
In systematic random Sampling, random starting point is chosen and after that from that random starting point systematic sampling is applied.
Approach:
Get data
Choose a random starting point
Apply systematic approach to the data
Perform operations as intended
Example: 
Python3
# Import in order to use inbuilt functionsimport numpy as npimport pandas as pdimport random # Define total number of housenumber_of_house = 30 # Create data dictionarydata = {'house_number': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,                         14, 15, 16, 17, 18, 19, 20, 21, 22, 23,                         24, 25, 26, 27, 28, 29, 30],        'number_of_children': [2, 2, 1, 3, 2, 1, 4, 1, 3, 5, 4, 3, 5,                               3, 2, 1, 2, 3, 4, 5, 3, 4, 5, 2, 2, 2,                               2, 3, 2, 1]} # Transform dictionary into a data framedf = pd.DataFrame(data) # Defining Size of Systematic Samplesize_of_systematic_sample = 6 # Defining Interval(gap) in order to get required data.interval = (number_of_house // size_of_systematic_sample) # Choosing Random Numberrandom_number = random.randint(1, 30) # Define systematic sampling functiondef systematic_sampling(df, step):     indexes = np.arange(random_number, len(df), step=step)    systematic_sample = df.iloc[indexes]    return systematic_sample  # Obtain a systematic sample and save it in a new variablesystematic_sample = systematic_sampling(df, interval) # View sampled data framedisplay(systematic_sample) # Empty Print Statement for new lineprint() # Save the sample data in a separate variablesystematic_data = round(systematic_sample['number_of_children'].mean()) # Printing Average Number of Childrenprint("Average Number Of Childrens in Locality: ", systematic_data)
Output:
Linear Systematic Sampling is a type of systematic sampling where the sample are selected using linear approach. Linear approach in the sense that after particular interval the sample is selected from the large data and after that operations are performed on the Selected Sample.
The elements are chosen between the range starting_random_number to last_element -1.
Approach:
Get data
Select data from the dataset after a particular interval
Perform operations as intended
Example:
Python3
# Import in order to use inbuilt functionsimport numpy as npimport pandas as pdimport random # Define total number of boxesnumber_of_boxes = 30 # Create data dictionarydata = {'Box_Number': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,                       15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26,                       27, 28, 29, 30],                 'Defective_Bulbs': [2, 2, 1, 0, 2, 1, 0, 1, 3, 5, 4, 3, 5, 3,                            0, 1, 2, 0, 4, 5, 3, 4, 5, 2, 0, 3, 2, 0,                            5, 4]} # Transform dictionary into a data framedf = pd.DataFrame(data)  # Size of Systematic Samplesize_systematic_sample = 5 # Interval (Gap) takeninterval = (number_of_boxes // size_systematic_sample) # Choosing Random Starting Pointrandom_number = random.randint(1, 30) # Define systematic sampling functiondef systematic_sampling(df, step):     indexes = np.arange(random_number, len(df)-1, step=step)    systematic_sample = df.iloc[indexes]    return systematic_sample  # Obtain a systematic sample and save it in a new variablesystematic_sample = systematic_sampling(df, interval) # View sampled data framedisplay(systematic_sample) # Empty Print Statement for new lineprint() # Save the sample data in a separate variablesystematic_data = round(systematic_sample['Defective_Bulbs'].mean()) # Printing Average Number of Defective Bulbsprint("Average Number Of Defective Bulbs: ", systematic_data)
Output:
In Circular Systematic Sampling, a sample again starts from the same point after ending. Basically, while selecting samples systematically and when ending element is reached, once again the selecting of sample will start from the beginning until all the elements of sample are selected. It means operations are performed on all the data which is selected using Circular Systematic Sampling.
Approach:
Get data
Select samples systematically
Once end is reached, restart
Perform operations as intended
Program:
Python3
# Import in order to use inbuilt functionsimport numpy as npimport pandas as pdimport random # Define total number of housenumber_of_house = 30 # Create data dictionarydata = {'house_number': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,                         14, 15, 16, 17, 18, 19, 20, 21, 22, 23,                         24, 25, 26, 27, 28, 29, 30],        'number_of_Adults': [2, 2, 5, 3, 2, 8, 4, 7, 8, 5, 4, 9, 5,                             4, 2, 3, 2, 3, 4, 5, 6, 4, 5, 4, 2, 6,                             2, 3, 2, 2]} # Transform dictionary into a data framedf = pd.DataFrame(data) # Defining Size of Systematic Samplesize_of_systematic_sample = 6  # Defining Interval(gap) in order to get required data.interval = (number_of_house // size_of_systematic_sample) # Define systematic sampling functiondef systematic_sampling(df, step):    indexes = np.arange(0, len(df), step=step)    systematic_sample = df.iloc[indexes]    return systematic_sample  # Obtain a systematic sample and save it in a new variablesystematic_sample = systematic_sampling(df, interval) # View sampled data framedisplay(systematic_sample)  # Empty Print Statement for new lineprint() # Save the sample data in a separate variablesystematic_data = round(systematic_sample['number_of_Adults'].mean()) # Printing Average Number of Childrenprint("Average Number Of Adults in Locality: ", systematic_data)
Output:
sagartomar9927
sumitgumber28
Picked
Python-pandas
Technical Scripter 2020
Python
Technical Scripter
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Python Classes and Objects
Python OOPs Concepts
Introduction To PYTHON
How to drop one or multiple columns in Pandas Dataframe
Python | os.path.join() method
Check if element exists in list in Python
How To Convert Python Dictionary To JSON?
Python | Get unique values from a list
Python | datetime.timedelta() function