Write a program in Python to generate a random array of 30 elements from 1 to 100 and calculate maximum by minimum of each row in a dataframe

In this tutorial, we'll learn how to generate a random array of 30 elements from 1 to 100, reshape it into a DataFrame, and calculate the ratio of maximum to minimum values for each row.

Understanding the Problem

We need to create a 6×5 DataFrame with random integers and calculate max/min ratio for each row using pandas operations.

Solution Approach

Follow these steps to solve the problem ?

  • Generate 30 random integers from 1 to 100 using np.random.randint()

  • Reshape the array to (6,5) to create a 2D structure

  • Convert to DataFrame and apply max/min calculation row-wise

  • Use df.apply() with axis=1 to process each row

Complete Implementation

Here's the complete code to generate the DataFrame and calculate maximum by minimum for each row ?

import pandas as pd
import numpy as np

# Generate random DataFrame with 30 elements reshaped to 6x5
df = pd.DataFrame(np.random.randint(1, 100, 30).reshape(6, 5))
print("Dataframe is:")
print(df)

# Calculate maximum by minimum for each row
max_by_min = df.apply(lambda x: np.max(x) / np.min(x), axis=1)
print("\nMaximum by minimum of each row:")
print(max_by_min)

The output will show a 6×5 DataFrame and the calculated ratios ?

Dataframe is:
    0   1   2   3   4
0  47  64  28  71  85
1  92  66  23  94  47
2  82  45  76  35  51
3  61  88  12  75  34
4  29  67  83  24  91
5  48  73  19  65  56

Maximum by minimum of each row:
0    3.035714
1    4.086957
2    2.342857
3    7.333333
4    3.793103
5    3.842105
dtype: float64

How It Works

The key components of this solution are ?

  • np.random.randint(1, 100, 30) ? Generates 30 random integers between 1 and 99

  • .reshape(6, 5) ? Converts 1D array to 6 rows and 5 columns

  • df.apply(lambda x: np.max(x) / np.min(x), axis=1) ? Applies function to each row

  • axis=1 ? Processes along rows (horizontally)

Alternative Using Built-in Methods

You can also use pandas built-in methods for better readability ?

import pandas as pd
import numpy as np

# Generate random DataFrame
df = pd.DataFrame(np.random.randint(1, 100, 30).reshape(6, 5))
print("Dataframe is:")
print(df)

# Calculate using pandas max() and min() methods
max_by_min = df.max(axis=1) / df.min(axis=1)
print("\nMaximum by minimum of each row:")
print(max_by_min)
Dataframe is:
    0   1   2   3   4
0  15  78  45  29  67
1  83  41  92  56  27
2  74  18  35  61  49
3  22  88  53  76  31
4  96  12  64  39  85
5  57  68  23  94  46

Maximum by minimum of each row:
0    5.200000
1    3.407407
2    4.111111
3    4.000000
4    8.000000
5    4.086957
dtype: float64

Conclusion

Use df.apply() with lambda functions for complex row-wise operations. For simple operations like max/min ratios, pandas built-in methods like df.max(axis=1) / df.min(axis=1) provide cleaner and more readable code.

Updated on: 2026-03-25T16:29:21+05:30

172 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements