Pandas Tutorial Pandas References

Pandas Series - iloc[] property



The Pandas iloc[] property is used for integer-location based (from 0 to length-1 of the axis) indexing for selection by position. It can 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 or DataFrame) and that returns valid output for indexing (one of the above).

Exceptions

Raises 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).

Example: Indexing rows of a DataFrame

In the example below, a DataFrame df is created. The iloc[] is used to index some rows of this DataFrame.

import pandas as pd
import numpy as np

df = pd.DataFrame({
  "Salary": [25, 24, 30, 28],
  "Bonus": [10, 8, 9, np.nan],
  "Others": [5, 4, 7, 5]},
  index= ["2015", "2016", "2017", "2018"]
)

print("The DataFrame is:")
print(df)

#indexing first row using an integer
print("\ndf.iloc[0] returns:")
print(df.iloc[0])
print(type(df.iloc[0]))

#indexing first row using a list
print("\ndf.iloc[[0]] returns:")
print(df.iloc[[0]])
print(type(df.iloc[[0]]))

#indexing first three row using a list
print("\ndf.iloc[[1,2,3]] returns:")
print(df.iloc[[1,2,3]])

#indexing first three row using slice object
print("\ndf.iloc[:3] returns:")
print(df.iloc[:3])

#indexing first and third rows using boolean array
#the boolean array must have same length as the index
print("\ndf.iloc[[True, False, True, False]] returns:")
print(df.iloc[[True, False, True, False]])

The output of the above code will be:

The DataFrame is:
      Salary  Bonus  Others
2015      25   10.0       5
2016      24    8.0       4
2017      30    9.0       7
2018      28    NaN       5

df.iloc[0] returns:
Salary    25.0
Bonus     10.0
Others     5.0
Name: 2015, dtype: float64
<class 'pandas.core.series.Series'>

df.iloc[[0]] returns:
      Salary  Bonus  Others
2015      25   10.0       5
<class 'pandas.core.frame.DataFrame'>

df.iloc[[1,2,3]] returns:
      Salary  Bonus  Others
2016      24    8.0       4
2017      30    9.0       7
2018      28    NaN       5

df.iloc[:3] returns:
      Salary  Bonus  Others
2015      25   10.0       5
2016      24    8.0       4
2017      30    9.0       7

df.iloc[[True, False, True, False]] returns:
      Salary  Bonus  Others
2015      25   10.0       5
2017      30    9.0       7

Example: Indexing both axes of a DataFrame

In the example below, the iloc[] is used to index both axes of given DataFrame.

import pandas as pd
import numpy as np

df = pd.DataFrame({
  "Salary": [25, 24, 30, 28],
  "Bonus": [10, 8, 9, np.nan],
  "Others": [5, 4, 7, 5]},
  index= ["2015", "2016", "2017", "2018"]
)

print("The DataFrame is:")
print(df)

#indexing element at (0,0)
print("\ndf.iloc[0, 0] returns:")
print(df.iloc[0, 0])

#indexing rows (first and third)
#and columns (second and third)
print("\ndf.iloc[[0, 2],[1, 2]] returns:")
print(df.iloc[[0, 2],[1, 2]])

#indexing whole second column
print("\ndf.iloc[:,1] returns:")
print(df.iloc[:,1])

#indexing rows (first three rows)
#and columns (first two columns)
print("\ndf.iloc[:3, :2] returns:")
print(df.iloc[:3, :2])

#indexing whole first and third columns 
#using boolean array
print("\ndf.iloc[:, [True, False, True]] returns:")
print(df.iloc[:, [True, False, True]])

The output of the above code will be:

The DataFrame is:
      Salary  Bonus  Others
2015      25   10.0       5
2016      24    8.0       4
2017      30    9.0       7
2018      28    NaN       5

df.iloc[0, 0] returns:
25

df.iloc[[0, 2],[1, 2]] returns:
      Bonus  Others
2015   10.0       5
2017    9.0       7

df.iloc[:,1] returns:
2015    10.0
2016     8.0
2017     9.0
2018     NaN
Name: Bonus, dtype: float64

df.iloc[:3, :2] returns:
      Salary  Bonus
2015      25   10.0
2016      24    8.0
2017      30    9.0

df.iloc[:, [True, False, True]] returns:
      Salary  Others
2015      25       5
2016      24       4
2017      30       7
2018      28       5

Example: using callable function

The example below demonstrates on how to use callable function with iloc[].

import pandas as pd
import numpy as np

df = pd.DataFrame({
  "Salary": [25, 24, 30, 28, 25],
  "Bonus": [10, 8, 9, np.nan, 9],
  "Others": [5, 4, 7, 5, 8]},
  index= [2015, 2016, 2017, 2018, 2019]
)

print("The DataFrame is:")
print(df)

#indexing rows if index is odd
print("\ndf.iloc[lambda x: x.index % 2 != 0] returns:")
print(df.iloc[lambda x: x.index % 2 != 0])

#indexing first and third columns
print("\ndf.iloc[:, lambda x: [0, 2]] returns:")
print(df.iloc[:, lambda x: [0, 2]])

The output of the above code will be:

The DataFrame is:
      Salary  Bonus  Others
2015      25   10.0       5
2016      24    8.0       4
2017      30    9.0       7
2018      28    NaN       5
2019      25    9.0       8

df.iloc[lambda x: x.index % 2 != 0] returns:
      Salary  Bonus  Others
2015      25   10.0       5
2017      30    9.0       7
2019      25    9.0       8

df.iloc[:, lambda x: [0, 2]] returns:
      Salary  Others
2015      25       5
2016      24       4
2017      30       7
2018      28       5
2019      25       8

Example: Setting values

The iloc[] can also be used to set the value of elements. Consider the example below:

import pandas as pd
import numpy as np

df = pd.DataFrame({
  "Salary": [25, 24, 30, 28, 25],
  "Bonus": [10, 8, 9, np.nan, 9],
  "Others": [5, 4, 7, 5, 8]},
  index= [2015, 2016, 2017, 2018, 2019]
)

print("The DataFrame is:")
print(df)

#setting the value of first column to 50
df.iloc[:,0] = 100
print("\n After modifying first column:")
print(df)

#setting the value of fourth row to 25
df.iloc[3] = 25
print("\n After modifying fourth row:")
print(df)

The output of the above code will be:

The DataFrame is:
      Salary  Bonus  Others
2015      25   10.0       5
2016      24    8.0       4
2017      30    9.0       7
2018      28    NaN       5
2019      25    9.0       8

 After modifying first column:
      Salary  Bonus  Others
2015     100   10.0       5
2016     100    8.0       4
2017     100    9.0       7
2018     100    NaN       5
2019     100    9.0       8

 After modifying fourth row:
      Salary  Bonus  Others
2015     100   10.0       5
2016     100    8.0       4
2017     100    9.0       7
2018      25   25.0      25
2019     100    9.0       8

❮ Pandas Series - Functions