Doubt: Using loops with Pandas

Screen Link: https://app.dataquest.io/m/292/exploring-data-with-pandas%3A-intermediate/11/using-loops-with-pandas

My Code:

top_employer_by_country = {}

countries = f500['country'].unique()
for c in countries:
    select_rows = f500[f500['country'] == c]  
    sorted_rows = select_rows.sort_values('employees', ascending = False)
    onee = sorted_rows.iloc[0]
    company = onee['company']
    top_employer_by_country[c] = company

When we iterate over the unique values in countries, does ‘select_rows’ group the dataframe data according to the current iteration in ‘countries’ and store them as seperate dataframes? Also, when we have sorted the dataframe according to the ‘employees’ column, how does Pandas sort the employees while keeping the ‘country’ column grouped?
Can someone explain the for loop steps in detail.
Thank Youu

Hi @sheikhhabib29

Answers to both your questions are within the question!

Yup, select_rows comes out as a separate dataframe for each of the country!

Try commenting out everything below the code: select_rows = f500[f500['country'] == c]
and just try print(type(select_row))

And since we have records of only one country in select_rows dataframe at a time, the sorting happens only for those many rows and doesn’t effect the entire f500 dataframe.

1 Like