To check whether female household income is higher than the average male house hold income (using dataframe given)

Viewed 28

This is the code that I have done to check whether females' household income is higher than males' average household income.

#Get total number of female customers
df_Female = df[df['Gender']=='Female']
FemaleIncomeArray = df_Female.loc[:,'Income'].values #get female income
FemaleIncomeList = FemaleIncomeArray.tolist() #convert to list

#Get male average household income
df_Male = df[df['Gender']=='Male']
MaleAvgIncome = df_Male.groupby('Gender')['Income'].mean()

color_list = []
y_pos=[] #y-axis positions
for i in range(len(FemaleIncomeList)):
    if FemaleIncomeList[i] >= MaleAvgIncome : #got error from this line
        color_list.append('y')
    else:
        color_list.append('r')
    y_pos.append(i+1) 

However, I got an error:

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
1 Answers

sometimes when you check a condition on series or dataframes, your output is a series such as ( , False). In this case you must use any, all, item,... use print function for your condition to see the series

Related