Data Analysis with Python Projects - Demographic Data Analyzer

Tell us what’s happening:
Working on cleaning up my code, so I still have comments everywhere.
I have 3 failures remaining, but just removed the last of my errors.

I was getting a Value Error (The truth value of a Series is ambiguous) when running the testing module. I was getting the correct answer by using value_counts() and .index.size(), but it appears was returning a Series and thus getting an error.

Can someone explain to me why using the len() works but the other solutions were not? (A mixed approach also threw TypeErrors of ‘int object is not callable’)

Your code so far
Original Solution – correct answer, but throws ValueError with test module:
round(((df.loc[df[‘education’] == ‘Bachelors’].value_counts(‘education’) / df.index.size) * 100),1)

New Solution:
# why does len work but not .index.size() or .value_counts()
percentage_bachelors = round((len(df.loc[df[‘education’] == ‘Bachelors’]) / len(df[‘education’])) * 100,1)

Challenge: Data Analysis with Python Projects - Demographic Data Analyzer

Link to the challenge:

len returns a number:

>>> percentage_bachelors = round((len(df.loc[df['education'] == 'Bachelors']) / len(df['education'])) * 100,1)


>>> type(percentage_bachelors)


This returns a Pandas Series Object:

>>> percentage_bachelors = round(((df.loc[df['education'] == 'Bachelors'].value_counts('education') / df.index.size) * 100),1)

<class 'pandas.core.series.Series'>
Index: 1 entries, Bachelors to Bachelors
Series name: None
Non-Null Count  Dtype  
--------------  -----  
1 non-null      float64
dtypes: float64(1)
memory usage: 16.0+ bytes

>>> type(percentage_bachelors)


You could derive and convert the object like this:

percentage_bachelors = float(percentage_bachelors[0])

Using type() or .info() in these cases to see what you’re returning can really help.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.