编写一个 Python 程序来修剪数据框中的最小和最大阈值
pythonpandasserver side programmingprogramming更新于 2024/6/22 10:52:17
假设您有一个数据框以及修剪最小和最大阈值的结果,
最小阈值: Column1 Column2 0 30 30 1 34 30 2 56 30 3 78 50 4 30 90 最大阈值: Column1 Column2 0 12 23 1 34 30 2 50 25 3 50 50 4 28 50 剪切的数据框为: Column1 Column2 0 30 30 1 34 30 2 50 30 3 50 50 4 30 50
解决方案
为了解决这个问题,我们将遵循下面给出的步骤 −
定义一个数据框
在 (lower=30) 内应用 df.clip 函数来计算最小阈值,
df.clip(lower=30)
在 (upper=50) 内应用 df.clip 函数来计算最大阈值
df.clip(upper=50)
应用具有最小和最大阈值限制的裁剪数据框,
df.clip(lower=30,upper=50)
示例
让我们检查以下代码以更好地理解 −
import pandas as pd
data = {"Column1":[12,34,56,78,28],
"Column2":[23,30,25,50,90]}
df = pd.DataFrame(data)
print("DataFrame 为:\n",df)
print("最小阈值:\n",df.clip(lower=30))
print("最大阈值:\n",df.clip(upper=50))
print("剪切的数据框为:\n",df.clip(lower=30,upper=50))
输出
DataFrame 为: Column1 Column2 0 12 23 1 34 30 2 56 25 3 78 50 4 28 90 最小阈值: Column1 Column2 0 30 30 1 34 30 2 56 30 3 78 50 4 30 90 最大阈值: Column1 Column2 0 12 23 1 34 30 2 50 25 3 50 50 4 28 50 剪切的数据框为: Column1 Column2 0 30 30 1 34 30 2 50 30 3 50 50 4 30 50
相关文章
有用资源
python 参考教程 - 该教程包含有关 python 的更多信息:https://www.cainiaomax.com/python/

