用 Python 编写一个程序,在给定的 DataFrame 中查找员工 ID 和薪水的最小年龄
输入-
假设你有一个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
输出-
并且,员工ID和工资的最低年龄的结果,
Id Salary 1 2 25000
解决方案
为了解决这个问题,我们将遵循以下方法。
定义数据帧
设置条件以检查等于最小年龄的DataFrameAge列。将其存储在结果DataFrame中。
result = df[df['Age']==df['Age'].min()]
从结果DataFrame中过滤Id和Salary。它定义如下,
result[['Id','Salary']]
例子
让我们看看下面的实现,以便更好地理解。
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']])输出
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
热门推荐
1 训练狗狗憋尿
2 狗狗追踪训练
3 狗狗训练要多少钱
5 深圳狗狗训练
6 训练狗狗唤回
7 狗狗叫怎么训练
8 狗狗训练班
10 狗狗社会化训练
11 狗狗叫回训练
12 广州狗狗训练学校
13 狗狗训练躺下
14 狗狗随行训练
15 训练狗狗巡回
16 狗狗要怎么训练
17 狗狗喂食训练
18 训练狗狗躺下