Write a Python program to find the maximum value from first four rows in a given series

A Pandas Series is a one-dimensional data structure that allows you to store and manipulate data efficiently. Finding the maximum value from specific rows is a common operation when analyzing data.

Problem Statement

Input ? Assume you have a Series:

0    11
1    12
2    66
3    24
4    80
5    40
6    28
7    50

Output ? Maximum value from first four rows is 66.

Solution

To solve this problem, we will follow these steps ?

  • Import pandas and create a Series

  • Use iloc[0:4] to select the first four rows

  • Apply the max() method to find the maximum value

Example

Let us see the complete implementation to get a better understanding ?

import pandas as pd

# Create a Series with sample data
data_list = [11, 12, 66, 24, 80, 40, 28, 50]
data = pd.Series(data_list)

print("Original Series:")
print(data)
print("\nFirst four rows:")

# Select first four rows using iloc
first_four_rows = data.iloc[0:4]
print(first_four_rows)

# Find maximum value from first four rows
max_value = first_four_rows.max()
print(f"\nMaximum value from first four rows: {max_value}")
Original Series:
0    11
1    12
2    66
3    24
4    80
5    40
6    28
7    50
dtype: int64

First four rows:
0    11
1    12
2    66
3    24
dtype: int64

Maximum value from first four rows: 66

How It Works

The iloc property allows integer-location based indexing. When we use data.iloc[0:4], it selects rows from index 0 to 3 (4 is excluded). The max() method then returns the largest value from this subset.

Alternative Approach

You can also use the head() method to achieve the same result ?

import pandas as pd

data_list = [11, 12, 66, 24, 80, 40, 28, 50]
data = pd.Series(data_list)

# Using head() method to get first 4 rows
max_value = data.head(4).max()
print(f"Maximum value using head(): {max_value}")
Maximum value using head(): 66

Conclusion

Use iloc[0:4].max() or head(4).max() to find the maximum value from the first four rows of a Pandas Series. Both approaches are efficient and provide the same result.

Updated on: 2026-03-25T15:53:39+05:30

179 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements