w3resource

Pandas Series property: iloc

Access a group of rows and columns in Pandas

The iloc property is used to access a group of rows and columns by label(s) or a boolean array.

.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 or DataFrame) 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).

Syntax:

Series.iloc
Pandas Series iloc property

Example :

Python-Pandas Code:

import numpy as np
import pandas as pd
mydict = [{'p': 2, 'q': 3, 'r': 4, 's': 5},
           {'p': 20, 'q': 30, 'r': 40, 's': 50},
           {'p': 200, 'q': 300, 'r': 400, 's': 500 }]
df = pd.DataFrame(mydict)
df

Output:

  p	q	r	s
0	2	3	4	5
1	20	30	40	50
2	200	300	400	500
Pandas Series iloc property

Example - Indexing just the rows:

With a scalar integer.

Python-Pandas Code:

import numpy as np
import pandas as pd
mydict = [{'p': 2, 'q': 3, 'r': 4, 's': 5},
           {'p': 20, 'q': 30, 'r': 40, 's': 50},
           {'p': 200, 'q': 300, 'r': 400, 's': 500 }]
df = pd.DataFrame(mydict)
type(df.iloc[0])

Output:

pandas.core.series.Series

Python-Pandas Code:

import numpy as np
import pandas as pd
mydict = [{'p': 2, 'q': 3, 'r': 4, 's': 5},
           {'p': 20, 'q': 30, 'r': 40, 's': 50},
           {'p': 200, 'q': 300, 'r': 400, 's': 500 }]
df = pd.DataFrame(mydict)
df.iloc[0]

Output:

p    2
q    3
r    4
s    5
Name: 0, dtype: int64
Pandas Series iloc property

Example - With a list of integers:

Python-Pandas Code:

import numpy as np
import pandas as pd
mydict = [{'p': 2, 'q': 3, 'r': 4, 's': 5},
           {'p': 20, 'q': 30, 'r': 40, 's': 50},
           {'p': 200, 'q': 300, 'r': 400, 's': 500 }]
df = pd.DataFrame(mydict)
df.iloc[[0]]

Output:

  p	q	r	s
0	2	3	4	5

Python-Pandas Code:

import numpy as np
import pandas as pd
mydict = [{'p': 2, 'q': 3, 'r': 4, 's': 5},
           {'p': 20, 'q': 30, 'r': 40, 's': 50},
           {'p': 200, 'q': 300, 'r': 400, 's': 500 }]
df = pd.DataFrame(mydict)
type(df.iloc[[0]])

Output:

pandas.core.frame.DataFrame

Python-Pandas Code:

import numpy as np
import pandas as pd
mydict = [{'p': 2, 'q': 3, 'r': 4, 's': 5},
           {'p': 20, 'q': 30, 'r': 40, 's': 50},
           {'p': 200, 'q': 300, 'r': 400, 's': 500 }]
df = pd.DataFrame(mydict)
df.iloc[[0, 2]]

Output:

  p	q	r	s
0	2	3	4	5
2	200	300	400	500
Pandas Series iloc property

Example - With a slice object:

Python-Pandas Code:

import numpy as np
import pandas as pd
mydict = [{'p': 2, 'q': 3, 'r': 4, 's': 5},
           {'p': 20, 'q': 30, 'r': 40, 's': 50},
           {'p': 200, 'q': 300, 'r': 400, 's': 500 }]
df = pd.DataFrame(mydict)
df.iloc[:3]

Output:

  p	q	r	s
0	2	3	4	5
1	20	30	40	50
2	200	300	400	500

Example - With a boolean mask the same length as the index:

Python-Pandas Code:

import numpy as np
import pandas as pd
mydict = [{'p': 2, 'q': 3, 'r': 4, 's': 5},
           {'p': 20, 'q': 30, 'r': 40, 's': 50},
           {'p': 200, 'q': 300, 'r': 400, 's': 500 }]
df = pd.DataFrame(mydict)
df.iloc[[True, False, True]]

Output:

  p	q	r	s
0	2	3	4	5
2	200	300	400	500

With a callable, useful in method chains. The x passed to the lambda is the DataFrame being sliced.

This selects the rows whose index label even.

Python-Pandas Code:

import numpy as np
import pandas as pd
mydict = [{'p': 2, 'q': 3, 'r': 4, 's': 5},
           {'p': 20, 'q': 30, 'r': 40, 's': 50},
           {'p': 200, 'q': 300, 'r': 400, 's': 500 }]
df = pd.DataFrame(mydict)
df.iloc[lambda x: x.index % 2 == 0]

Output:

  p	q	r	s
0	2	3	4	5
2	200	300	400	500

Example - Indexing both axes:

You can mix the indexer types for the index and columns. Use : to select the entire axis. With scalar integers.

Python-Pandas Code:

import numpy as np
import pandas as pd
mydict = [{'p': 2, 'q': 3, 'r': 4, 's': 5},
           {'p': 20, 'q': 30, 'r': 40, 's': 50},
           {'p': 200, 'q': 300, 'r': 400, 's': 500 }]
df = pd.DataFrame(mydict)
df.iloc[0, 2]

Output:

4
Pandas Series iloc property

Example - With lists of integers:

Python-Pandas Code:

import numpy as np
import pandas as pd
mydict = [{'p': 2, 'q': 3, 'r': 4, 's': 5},
           {'p': 20, 'q': 30, 'r': 40, 's': 50},
           {'p': 200, 'q': 300, 'r': 400, 's': 500 }]
df = pd.DataFrame(mydict)
df.iloc[[0, 2], [1, 3]]

Output:

  q	s
0	3	5
2	300	500
Pandas Series iloc property

Example - With slice objects:

Python-Pandas Code:

import numpy as np
import pandas as pd
mydict = [{'p': 2, 'q': 3, 'r': 4, 's': 5},
           {'p': 20, 'q': 30, 'r': 40, 's': 50},
           {'p': 200, 'q': 300, 'r': 400, 's': 500 }]
df = pd.DataFrame(mydict)
df.iloc[1:3, 0:3]

Output:

   p	q	r
1	20	30	40
2	200	300	400

Example - With a boolean array whose length matches the columns:

Python-Pandas Code:

import numpy as np
import pandas as pd
mydict = [{'p': 2, 'q': 3, 'r': 4, 's': 5},
           {'p': 20, 'q': 30, 'r': 40, 's': 50},
           {'p': 200, 'q': 300, 'r': 400, 's': 500 }]
df = pd.DataFrame(mydict)
df.iloc[:, [True, False, True, False]]

Output:

   p	r
0	2	4
1	20	40
2	200	400

Example - With a callable function that expects the Series or DataFrame:

Python-Pandas Code:

import numpy as np
import pandas as pd
mydict = [{'p': 2, 'q': 3, 'r': 4, 's': 5},
           {'p': 20, 'q': 30, 'r': 40, 's': 50},
           {'p': 200, 'q': 300, 'r': 400, 's': 500 }]
df = pd.DataFrame(mydict)
df.iloc[:, lambda df: [0, 2]]

Output:

   p	r
0	2	4
1	20	40
2	200	400

Previous: Access a group of rows and columns in Pandas
Next: Lazily iterate over tuples in Pandas



Follow us on Facebook and Twitter for latest update.