2
votes

The following code gives me Value Error:

major_males=[]

for row in recent_grads:
    if recent_grads['Men']>recent_grads['Women']:
        major_males.append(recent_grads['Major'])
display(major_males)  

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

3

3 Answers

1
votes

If recent_grads is a dataframe, then this is how your for loop would look like

major_males=[]

for i, row in recent_grads.iterrows():
    if row['Men']>row['Women']:
        major_males.append(row['Major'])
display(major_males)  
2
votes

That is because you are comparing a series and not a value. I guess your intension was if row['Men'] > row['Women']

Secondly it will be more efficient to do


major_males = recent_grads[recent_grads.Men > recent_grads.Women].Major.to_list()
0
votes

Note that, while you're iterating over the data frame, you're not using the row variable. Instead, try:

major_males=[]

for row in recent_grads:
    if row['Men']>row['Women']:
        major_males.append(row['Major'])
display(major_males)  

You get the error because it's not meaningful to compare all the Men values to all the Women values: instead you want to compare one specific value of each at a time, which is what the change does.