Why does this return a series full of company metadata?

Screen Link:

My Code:

#create new series in f500 df which contains values of return on assets; ROA = profits/assets
f500['roa'] = f500.loc[:,'profits'] / f500.loc[:,'assets']

#extract unique sectors for loop purposes
unique_sectors = f500.loc[:,'sector'].unique()

#empty dictionary to contain count agg of ROA by sector
top_roa_by_sector = {}

#loop to append results to dictionary
for x in unique_sectors:
    select = f500.loc[f500.loc[:,'sector'] == x,:]
    sort = select.sort_values('roa', ascending=False).iloc[0]
    company_names = sort['company']
    top_roa_by_sector[x] = company_names

I am just curiious about the sort = select.sort_values('roa', ascending=False).iloc[0] line. Why does this produce a series with all the metadata about the companies when I explicitly called .iloc[0]

I expect it to generate a series but only containing company names or such. Thank you!

1 Like

Hi @la.cayabyab,

In that line we first sorted out the values of select in descending order by 'roa', so the row with the maximum 'roa' became the first one (i.e., row [0]). Then, by applying iloc[0], we chose exactly that row with the maximum 'roa', with all the columns, containing information about the company represented in that row (“Adecco Group”). Then, in the next line of code

company_names = sort['company']

we selected from all the metadata of the company “Adecco Group” only its name.

So we were not going to obtain a list of company names, but only the name of the company with the highest 'roa'.

1 Like