Pandas 数据帧clip()
方法
原文:https://www.studytonight.com/pandas/pandas-dataframe-clip-method
在本教程中,我们将学习 PandasDataFrame.clip()
方法。该方法在输入阈值处调整值。它将边界外的值指定给边界值。阈值可以是奇异值或类似数组的值,在后一种情况下,在指定的轴上按元素执行裁剪。
下图显示了DataFrame.clip()
方法的语法。
句法
DataFrame.clip(lower=None, upper=None, axis=None, inplace=False, *args, **kwargs)
因素
下限:表示浮点或 array_like,默认无。它表示最小阈值,低于该阈值的所有值都将被设置为它。
上:代表浮动或 array_like,默认无。它代表最大阈值。高于此阈值的所有值都将被设置为该值。
轴:表示 int 或 str 轴名称,可选。它沿着给定的轴上下对齐对象。
到位:表示 bool(真或假),默认值为 False。是否对数据就地执行操作。
*args,kwargs** :是额外的关键词,没有效果,但可能会被接受与 NumPy 兼容。
示例 1:具有upper
阈值的DataFrame.clip()
方法
以下示例显示了DataFrame.clip()
方法如何在upper
阈值处调整值。
#importing pandas library
import pandas as pd
data = {'col_1': [9, -3, 0, -1, 12], 'col_2': [-2, -7, -6, 8, -5]}
df = pd.DataFrame(data)
print("------DataFrame--------")
print(df)
print("------After clipping the DataFrame--------")
print(df.clip(upper=6))
一旦我们运行程序,我们将得到以下结果。
-数据帧- col 1 col 2 0 9-2 1-3-7 2 0-6 3-1 8 4 12-5 -剪切数据帧后- col 1 col 2 0 6-2 1-3-7 2 0-6 3-1 6 4 6-5
示例 2:具有lower
阈值的DataFrame.clip()
方法
以下示例显示了DataFrame.clip()
方法如何在lower
阈值处调整值。
#importing pandas library
import pandas as pd
data = {'col_1': [9, -3, 0, -1, 12], 'col_2': [-2, -7, -6, 8, -5]}
df = pd.DataFrame(data)
print("------DataFrame--------")
print(df)
print("------After clipping the DataFrame--------")
print(df.clip(lower=-1))
一旦我们运行程序,我们将得到以下结果。
-数据帧- col 1 col 2 0 9-2 1-3-7 2 0-6 3-1 8 4 12-5 -剪切数据帧后- col 1 col 2 0 9-1 1-1-1 2 0-1 3-1 8 4 12-1
示例 3:具有lower
和upper
阈值的DataFrame.clip()
方法
以下示例显示了DataFrame.clip()
方法如何在lower
和upper
阈值处修剪值。
#importing pandas library
import pandas as pd
data = {'col_1': [9, -3, 0, -1, 12], 'col_2': [-2, -7, -6, 8, -5]}
df = pd.DataFrame(data)
print("------DataFrame--------")
print(df)
print("------After clipping the DataFrame--------")
print(df.clip(-1,6))
一旦我们运行程序,我们将得到以下结果。
-数据帧- col 1 col 2 0 9-2 1-3-7 2 0-6 3-1 8 4 12-5 -剪切数据帧后- col 1 col 2 0 6-1 1-1-1 2 0-1 3-1 6 4 6-1
示例 4:具有lower
、upper
阈值和inplace=True
的DataFrame.clip()
方法
下面的例子显示了inplace=True
时DataFrame.clip()
方法如何在lower
和upper
阈值处修剪值。
DataFrame.clip()
方法逐元素修剪值,如果inplace=True.
则返回None
#importing pandas library
import pandas as pd
data = {'col_1': [9, -3, 0, -1, 12], 'col_2': [-2, -7, -6, 8, -5]}
df = pd.DataFrame(data)
print("------DataFrame--------")
print(df)
print("------After clipping the DataFrame--------")
print(df.clip(-1,6,inplace=True))
print(df)
一旦我们运行程序,我们将得到以下结果。
-数据帧- col 1 col 2 0 9-2 1-3-7 2 0-6 3-1 8 4 12-5 -剪切数据帧后- 无 col 1 col 2 0 6-1 1-1-1 2 0-1 3-1 6 4 6-1
结论
在本教程中,我们学习了DataFrame.clip()
方法。我们学习了语法、参数,通过解决不同的例子,我们理解了DataFrame.clip()
方法。