Write a program in Python to find the minimum age of an employee id and salary in a given DataFrame

PythonPandasServer Side ProgrammingProgramming

Input

Assume, you have a DataFrame

DataFrame is
 Id    Age   Salary
0 1    27    40000
1 2    22    25000
2 3    25    40000
3 4    23    35000
4 5    24    30000
5 6    32    30000
6 7    30    50000
7 8    28    20000
8 9    29    32000
9 10   27    23000

Output

And, the result for a minimum age of an employee id and salary,

 Id Salary
1 2 25000

Solution

To solve this, we will follow the below approaches.

  • Define a DataFrame

  • Set the condition to check the DataFrame Age column which is equal to minimum age. Store it in result DataFrame.

result = df[df['Age']==df['Age'].min()]
  • Filter Id and Salary from result DataFrame. It is defined below,

result[['Id','Salary']]

Example

Let us see the following implementation to get a better understanding.

import pandas as pd
data = [[1,27,40000],[2,22,25000],[3,25,40000],[4,23,35000],[5,24,30000],
[6,32,30000],[7,30,50000],[8,28,20000],[9,29,32000],[10,27,23000]]
df = pd.DataFrame(data,columns=('Id','Age','Salary'))
print("DataFrame is\n",df)
print("find the minimum age of an employee id and salary\n")
result = df[df['Age']==df['Age'].min()]
print(result[['Id','Salary']])

Output

DataFrame is
 Id    Age   Salary0 1 27 40000
1 2    22    25000
2 3    25    40000
3 4    23    35000
4 5    24    30000
5 6    32    30000
6 7    30    50000
7 8    28    20000
8 9    29    32000
9 10   27    23000
find the minimum age of an employee id and salary
 Id Salary
1 2 25000
raja
Published on 10-Feb-2021 13:30:55
Advertisements