Informatics Practices · Ch 3 — Data Handling using Pandas – II
Calculating Minimum Values
Calculating Minimum Values
The min() method on a DataFrame returns the smallest value from each column (or each row, if you specify the axis). It works across all data types — for numbers it gives the numerical minimum, and for strings it gives the alphabetically smallest value.
When you call df.min() without any arguments, it computes the minimum of every column in the DataFrame. For example, if you have a DataFrame df containing student marks, print(df.min()) will show the lowest mark in each subject column, and also the alphabetically first name in the Name column.
The output of df.min() is a Series, with the column names as the index and the minimum values as the data. The dtype of the result will be object if any column contains strings, because Pandas treats the entire Series as having a mixed type.
Finding Minimum Marks for a Specific Student
The textbook demonstrates this with a practical example: finding the minimum marks obtained by the student 'Mishti' across all unit tests for each subject.
First, you filter the DataFrame to get only the rows where the Name column equals 'Mishti':
dfMishti = df.loc[df.Name == 'Mishti']
This creates a new DataFrame dfMishti containing only Mishti's records. Printing it shows all her unit test scores:
| Name | UT | Maths | Science | S.St | Hindi | Eng |
|---|---|---|---|---|---|---|
| Mishti | 1 | 15 | 22 | 25 | 22 | 22 |
| Mishti | 2 | 18 | 21 | 25 | 24 | 23 |
| Mishti | 3 | 17 | 18 | 20 | 25 | 20 |
Now, to get the minimum marks in each subject (ignoring the UT column), you select only the subject columns and apply min(): |
dfMishti[['Maths','Science','S.St','Hindi','Eng']].min()
The output is:
Maths 15
Science 18
S.St 20
Hindi 22
Eng 20
dtype: int64
``` …
| Column | Min Value |
|---|---|
| Name | Ashravy |
| UT | 1 |
| Maths | 12 |
| Science | 15 |
| S.St | 15 |
| Hindi | 15 |
Write the statements to display the minimum marks obtained by a particular student 'Mishti' in all the unit tests for each subject. This shows min() applied to a student-filtered slice of the DataFrame, selecting only the subject …