Skip to content

Instantly share code, notes, and snippets.

@nsrCodes
Created March 25, 2021 04:46
Show Gist options
  • Save nsrCodes/58f91d5747c24968050f9cb25bf1aafb to your computer and use it in GitHub Desktop.
Save nsrCodes/58f91d5747c24968050f9cb25bf1aafb to your computer and use it in GitHub Desktop.
Python script to add a bmi column to the end of an excel sheet that was generated from a google form submissionsexcel sheet
'''
This is a script written to calculate bmi from form responses.
'''
import pandas as pd
# Constants
source_file_name = "data.xlsx"
form_name = 'Form Responses 1'
height_column_name = ' 1. Height in cm'
weight_column_name = ' 2. Weight'
destination_file_name = 'with_bmi.xlsx'
# in kg and cm
def calculate_bmi (h, w) :
if h!= 0:
h_in_m = h/100
return w / ( h_in_m*h_in_m )
data_as_dataframe = pd.read_excel(source_file_name, sheet_name=None)
df = data_as_dataframe[form_name][[height_column_name, weight_column_name]]
raw_height_weight = df.values.tolist()
bmi_data = []
for raw_data in raw_height_weight:
height, weight = raw_data[0], raw_data[1]
if (type(height)==type("string")):
height = float(height.split('c')[0])
if (type(weight)==type("string")):
weight = float(weight.lower().split('k')[0])
if(type(height)==type(0.02)):
height = int(height)
if(type(weight)==type(0.02)):
weight = int(weight)
bmi_data.append(calculate_bmi(height, weight))
data_as_dataframe['Form Responses 1']['bmi'] = bmi_data
data_as_dataframe['Form Responses 1'].to_excel(destination_file_name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment