• Post author:
  • Post category:Pandas
  • Post last modified:March 27, 2024
  • Reading time:16 mins read
You are currently viewing Pandas Series iloc[] Function

Pandas Series.iloc[] function is used to access elements in a Series by integer location. It allows you to access elements by their position in the Series, regardless of their index labels. You can also use the iloc[] function to select rows or columns from a DataFrame. In this article, I will explain the Series.iloc[] function and using its syntax and usage how we can access elements based on their integer positions with multiple examples.

Advertisements

Key Points –

  • iloc[] provides efficient integer-based indexing for accessing elements in a pandas Series, making it useful for positional data manipulation.
  • It allows for direct access to elements in a Series by their integer position, enabling tasks such as slicing, selecting specific elements, and working with numerical data efficiently.
  • You can use iloc[] to access single elements, slices, or a selection of elements based on their integer positions.
  • Negative integers can be used to index from the end of the Series.
  • It’s particularly useful when you need to work with data based on its position rather than its label.

Use Series.iloc[] to Access a Single Element by Position

To access a single element by its position in a Pandas Series, you can use the iloc[] function.

First, let’s create a Pandas Series from a list.


import pandas as pd

# Create a sample Series
series = pd.Series([10, 20, 30, 40, 50])
print("Original Series:\n",series)

Yields below output.

pandas series iloc

You can access a specific element in a Series by index position using series.iloc[] property. Apply this property to a given Series by passing the specified index position which is the particular location of the specific element. Remember that indexing in Python starts from zero, so the first element is at position 0, the second at position 1, and so on.


# Access the second element (position 1) using iloc
second_element = series.iloc[1]
print("Second element of the series:", second_element)

In this example, series.iloc[1] accesses the second element (index 1) of the Series, which has the value 20. This example yields the below output.

pandas series iloc

Apply Slicing Using Pandas Series.iloc[]

Alternatively, you can access a particular selection of elements by applying the slicing technique using the iloc[] function. Slicing means setting the particular range by specifying the start and stop positions.


# Access a slice of elements from position 1 to 3 
# Using iloc
ser2 = series.iloc[1:4]
print("Range of elements from position 1 to 3:\n",ser2)

# Output:
# Range of elements from position 1 to 3:
# 1    20
# 2    30
# 3    40
# dtype: int64

In the above example, series.iloc[1:4] accesses a particular portion of elements from position 1 to 4 (exclusive), and it returns a new Series containing elements 20, 30, and 40.

Using Negative Indexing of Pandas Series iloc[]

Similarly, you can pass negative indexing into the iloc[] property to access the elements of a Pandas Series from the ending position. The ending index position starts from -1.


# Access the last element 
# Using iloc with negative indexing
ser2 = series.iloc[-1]
print("Get last element of the series:", ser2)

# Output:
# Get last element of the series: 50 

In the above example, series.iloc[-1] accesses the last element of the Series, which has the value 50. Negative indexing allows you to access elements from the end of the Series, where -1 refers to the last element, -2 refers to the second-to-last element, and so on.

Access Multiple Elements using iloc[]

You can pass a list of index positions within the iloc[] function to access multiple elements from a Pandas Series. Let’s pass specified index positions into this property, to get the corresponding elements of passed indexes.


# Access elements at positions 0, 3, and 4 using iloc
positions = [0, 3, 4]
ser2 = series.iloc[positions]
print("Get selected elements at positions 0, 3, and 4:\n",ser2)

# Output:
# Get selected elements at positions 0, 3, and 4:
# 0    10
# 3    40
# 4    50
# dtype: int64

In the above example, series.iloc[positions] access the elements at positions 0, 3, and 4 of the Series, as specified by the list positions. It returns a new Series containing these elements.

Accessing Every nth Element

Similarly, to access every nth element in a Pandas Series use the iloc[] function with step notation. You can simply specify the increment size using the step param within the slice notation. It will return the elements of the Series with a specified step size.


# Access every second element starting from index 0 
# Using iloc with step
ser2 = series.iloc[::2]
print("Selected elements with step size 2:\n",ser2)

# Output:
# Selected elements with step size 2:
# 0    10
# 2    30
# 4    50
# dtype: int64

In the above example, series.iloc[::2] accesses every second element of the Series starting from index 0. The ::2 notation specifies a step size of 2, indicating that every second element should be selected.

Use Pandas Series.iloc[] and Lambda

Finally, you can use Series.iloc[] property along with lambda functions to filter elements based on their integer positions.


# Filter elements at even integer positions 
# Using iloc[] and lambda
ser2 = series.iloc[lambda x: x.index % 2 == 0]
print(ser2)

# Output:
# 0    10
# 2    30
# 4    50
# dtype: int64

In the above example, the lambda function lambda x: x.index % 2 == 0 is used inside iloc[] to filter elements at even integer positions. The lambda function returns a boolean array where True corresponds to even integer positions and False corresponds to odd integer positions. Therefore, only elements at even positions will be selected.

Frequently Asked Questions on Pandas Series iloc[] Function

What is the purpose of the iloc[] function in Pandas Series?

The iloc[] function in Pandas Series is used to access elements by their integer position. It allows for positional-based indexing, ignoring the index labels.

Can iloc[] be used to access multiple elements at once?

The iloc[] function in Pandas Series can indeed be used to access multiple elements at once. You can achieve this by specifying a range of positions or by providing a list of positions.

Can negative indices be used with iloc[]?

Negative indices can be used with the iloc[] function in Pandas Series. Negative indices allow to access elements from the end of the Series, counting backward.

How can I access every nth element using iloc[]?

To access every nth element in a Pandas Series using the iloc[] function, you can specify a step size within the slice notation, like series.iloc[::n].

Is iloc[] inclusive or exclusive for slicing?

The iloc[] function in Pandas Series is exclusive for slicing, meaning that the end position specified is not included in the result. When you specify a range of positions using iloc[], such as iloc[start:end], the element at the start position is included, but the element at the end the position is not.

How does iloc[] differ from loc[]?

iloc[] is used for integer-based indexing, while loc[] is used for label-based indexing. iloc[] accesses elements by their position, while loc[] accesses elements by their index labels.

Conclusion

In this article, you have learned about pandas Series iloc[] property. The iloc[] function in Pandas Series is a powerful tool for accessing elements based on their integer positions. It provides flexibility for selecting single elements, slices of elements, or even every nth element with examples.

Happy Learning !!

Related Articles

References

Malli

Malli is an experienced technical writer with a passion for translating complex Python concepts into clear, concise, and user-friendly articles. Over the years, he has written hundreds of articles in Pandas, NumPy, Python, and takes pride in ability to bridge the gap between technical experts and end-users.