Data Analysis with Python Projects - Demographic Data Analyzer

Tell us what’s happening:
When should you use loc?
My question specifically revolves around:
“# What is the minimum number of hours a person works per week (hours-per-week feature)?”

If I say:
min_work_hours = df[‘hours-per-week’].min()
then I get a valid minimum

If I say:
min_work_hours = df.loc[‘hours-per-week’].min()
then I get a KeyError.

I thought that option 2 was simply stating the same thing as option 1, but explicitly.
Can someone explain when I should be using option 1 vs option 2?

Your code so far

Challenge: Data Analysis with Python Projects - Demographic Data Analyzer

Link to the challenge:

The .loc() method is used to access rows, not columns.
It functions identically to .iloc() if the dataframe uses numerical indexing.

I see – that makes sense!
So when we are identifying a row we must either use iloc() to select the row using its’ integer position or loc() to select the row using its’ label?
Then for selecting columns we do not have to explicitly state any commands?

Right, just use brackets for columns.

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