In: Computer Science
Consider below dataframe, df2, which provides the number of males and females in a population as well as their socio-economic breakdown. Provide python codes to calculate following probabilities:
df2 = pd.DataFrame({"M": [30,250,20], "F":[10,150,40]}, index=["upperMiddle", "middle", "working"])
df2
RAW CODE
import pandas as pd
df2 = pd.DataFrame({"M":[30,250,20],"F":[10,150,40]},index = ["UpperMiddle","middle","working"])
#randomly selected person from this population, probability of being female ("F")
total_females = df2.iloc[:,1].sum() ### female is at index 1 of column. (summing all the females present)
total_peoples = df2.iloc[:,0].sum() + df2.iloc[:,1].sum() ### Sum of total population
print("Probability of being female: ",total_females/total_peoples) ### Probability = favourable/total_outcomes
#randomly selected person from this population, probability of being working class ("working")
total_working_class = df2.iloc[2,:].sum() ### Working is at the index 2, we can also use -1 instead of 2 as it is last index.
print("Probability of being working class: ",total_working_class/total_peoples)
#randomly selected person from this population, joint probability of being male ("M") and middle class ("middle")
total_male_middle_class = df2.iloc[1,0] ### middle class row is at 1 index and Male is at 0 index. (Just one value so no need of sum)
print("Probability of being middle class male: ",total_male_middle_class/total_peoples)
SCREENSHOTS (CODE AND OUTPUT)
CODE
OUTPUT
##### FOR ANY QUERY, KINDLY GET BACK, THANKYOU. #####