用 Python 编写一个程序,在给定的 DataFrame 中查找员工 ID 和薪水的最小年龄
pythonpandasserver side programmingprogramming更新于 2024/6/19 8:44:17
输入 −
假设,你有一个 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
解决方案
为了解决这个问题,我们将遵循以下方法。
定义 DataFrame
设置条件以检查 DataFrame Age 列是否等于最低年龄。将其存储在结果 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
相关文章
有用资源
python 参考教程 - 该教程包含有关 python 的更多信息:https://www.cainiaomax.com/python/

