Using of .iloc()

In the answer to this mission, they used

sorted_rows.iloc[0]['company']

I was wondering how come ['company'] works in .iloc when it is supposed to only use integers to index?

Thanks

["company"] is not part of the .iloc. You first extract the first row using

sorted_rows.iloc[0]

The above essentially returns a DataFrame with 1 row. And from that DataFrame you extract the value for the company column.

Just like you would do for a normal DataFrame (df, for example) -

df["company"]

The above would return all values for the column company in the DataFrame df.

2 Likes

Okay this makes a lot of sense. Thanks for the clarification!

1 Like

I would like to clarify, that based on the Documentation,

sorted_rows.iloc[0]

is not technically a DataFrame, it would be more like a Series. But the concept of extracting the value for the company column would still be the same in that case. If you check the documentation, if you wanted to return it as a DataFrame, the code would change to -

sorted_rows.iloc[[0]]

Just a small clarification, but works the same essentially I believe. Experiment with a few outputs if you’d like to understand this better, I would say.

2 Likes