pandas.DataFrame.iloc() function with example in python | 2019
pandas.DataFrame.iloc(): This function used for purely integer-location based indexing for selection by position.
.iloc[] is primarily integer position based (from 0 to length-1 of the axis), but may also be used with a boolean array.
Allowed inputs are:
An integer, e.g. 5.
A list or array of integers, e.g. [4, 3, 0].
A slice object with ints, e.g. 1:7.
A boolean array.
A callable function with one argument (the calling Series, DataFrame or Panel) and that returns valid output for indexing (one of the above). This is useful in method chains, when you don’t have a reference to the calling object, but would like to base your selection on some value.
.iloc will raise IndexError if a requested indexer is out-of-bounds, except slice indexers which allow out-of-bounds indexing (this conforms with python/numpy slice semantics).
pandas.DataFrame.iloc() function example
import pandas as pd
df = pd.DataFrame([[1, 2], [4, 5], [7, 8]], index=['raju', 'ravi', 'dani'], columns=['exp', 'salary'])
print(df.iloc[0])
print(df.iloc[[0]])
print(df.iloc[:1])
print(df.iloc[1:2])
print(df.iloc[0:1])
Output:
exp 1 salary 2 Name: raju, dtype: int64 exp salary raju 1 2 exp salary raju 1 2 exp salary ravi 4 5 exp salary raju 1 2