iloc
noun
say it: “EYE-lohk” /ˈaɪˌloʊk/
In pandas, the way to pick rows and columns of a table by their position, counting from 0.
What does iloc do in pandas?
iloc, read "i loc", picks rows and columns by their position, counting from 0, as df.iloc[rows, columns]. A
slice of positions stops before its end, as Python's slices do, so iloc[0:3] is the first three rows.
print(products.iloc[0]) # the first row
print(products.iloc[0:3, 0:2]) # the first three rows, first two columns
In a sentence
- “iloc took the first three rows by position.”
- “After the sort, iloc gave a different first row, because positions change.”
Level: Journeyman · More Python terms at this level
Updated