The document contains code to calculate the mean, median, and mode of group data from excel files. It imports pandas, reads in excel files, splits interval columns, calculates midpoints and sums, and prints the final mean, median, and mode values. The mean is calculated by finding the sum of the frequency multiplied by midpoint values divided by the total frequency sum. The median finds the value where the cumulative sum of frequencies is greater than half the total frequency. The mode finds the interval with the maximum frequency and calculates the value within that interval.
Original Description:
Python Code foe Mean, Mode and Median of Group Data
The document contains code to calculate the mean, median, and mode of group data from excel files. It imports pandas, reads in excel files, splits interval columns, calculates midpoints and sums, and prints the final mean, median, and mode values. The mean is calculated by finding the sum of the frequency multiplied by midpoint values divided by the total frequency sum. The median finds the value where the cumulative sum of frequencies is greater than half the total frequency. The mode finds the interval with the maximum frequency and calculates the value within that interval.
The document contains code to calculate the mean, median, and mode of group data from excel files. It imports pandas, reads in excel files, splits interval columns, calculates midpoints and sums, and prints the final mean, median, and mode values. The mean is calculated by finding the sum of the frequency multiplied by midpoint values divided by the total frequency sum. The median finds the value where the cumulative sum of frequencies is greater than half the total frequency. The mode finds the interval with the maximum frequency and calculates the value within that interval.
import pandas as pd df=pd.read_excel("file_1.xlsx") df[["Lower","Upper"]]=df["class_Interval"].str.split("-",expand=True) df["Midpoint"]=(df["Lower"].astype("float")+df["Upper"].astype("float"))/2 df["fy"]=df["Frequency"]*df["Midpoint"] sum_fy=df["fy"].sum() sum_f=df["Frequency"].sum() Mean=(sum_fy/sum_f) print("The value of Mean is =", Mean)
In [ ]: # Median of Group Data
import pandas as pd df=pd.read_excel("file_2.xlsx") df[["Lower","Upper"]]=df["Class_interval"].str.split("-",expand=True) df["LCB"]=df["Lower"].astype("float")-0.5 df["UCB"]=df["Upper"].astype("float")+0.5 df["cum_sum"]=df["Frequency"].cumsum() nth_frequency=df["Frequency"].sum() n=nth_frequency/2 for i in range (0,6): if df["cum_sum"][i]>n: break cf=df["cum_sum"][i-1] l=df["LCB"][i] h=(df["LCB"][1]-df["LCB"][0]) f=df["Frequency"][i] MEDIAN=(l+(h/f)*(n-cf)) print("The Median is =",MEDIAN)
In [ ]: # Mode of Group Data
import pandas as pd df=pd.read_excel("file_4.xlsx") df[["Lower","Upper"]]=df["class_interval"].str.split("-",expand=True) df["LCB"]=df["Lower"].astype("float")-0.5 df["UCB"]=df["Upper"].astype("float")+0.5 fm=df["frequency"].max() i=df[df["frequency"]==fm].index[0] l=df["LCB"][i] h=(df["LCB"][1]-df["LCB"][0]) f1=df["frequency"][i-1] f2=df["frequency"][i+1] mode=l+((fm-f1)/(fm-1)+(fm-f2))*h print("The Mode is =",mode)