Python是进行数据分析的一种出色语言,主要是因为以数据为中心的python软件包具有奇妙的生态系统。 Pandas是其中的一种,使导入和分析数据更加容易。
 Pandas  dataframe.clip_upper()用于在指定的输入阈值处修整值。我们使用此函数将高于输入值阈值的所有值修整为指定的输入值。
用法: DataFrame.clip_upper(threshold, axis=None, inplace=False)
参数:
threshold:浮点数或数组
float :将每个值与阈值进行比较。
array-like :阈值的形状应与被比较的对象匹配。当self为Series时,阈值应为长度。当self是DataFrame时,阈值应为2-D,并且对于axis = None,其形状应与self相同,或者为1-D,且长度与要比较的轴相同。
axis:沿给定轴将对象与阈值对齐。
inplace:是否对数据执行适当的操作。
返回:剪切:与输入类型相同
范例1:采用clip_upper()用于将数据帧的值修整到给定阈值以上的函数。
# importing pandas as pd 
import pandas as pd 
  
# Creating a dataframe using dictionary 
df = pd.DataFrame({"A":[-5, 8, 12, -9, 5, 3], 
                   "B":[-1, -4, 6, 4, 11, 3], 
                   "C":[11, 4, -8, 7, 3, -2]}) 
  
# Printing the data frame for visualization 
df
现在,将所有大于8的值修剪为8。
# Clip all values below 2 
df.clip_upper(8)输出:

范例2:采用clip_upper()用于将 DataFrame 中的值裁剪为该 DataFrame 中每个单元格具有特定值的函数。
为此,我们可以使用numpy数组,但是数组的形状必须与 DataFrame 的形状相同。
# importing pandas as pd 
import pandas as pd 
  
# Creating a dataframe using dictionary 
  
df = pd.DataFrame({"A":[-5, 8, 12, -9, 5, 3],  
                   "B":[-1, -4, 6, 4, 11, 3], 
                   "C":[11, 4, -8, 7, 3, -2]}) 
  
# upper limit for each individual column element. 
limit = np.array([[10, 2, 8], [3, 5, 3], [2, 4, 6], 
                  [11, 2, 3], [5, 2, 3], [4, 5, 3]]) 
  
# Print upper_limit 
limit
现在将这些限制应用于 DataFrame 。
# applying different limit value 
# for each cell in the dataframe 
df.clip_upper(limit)输出:

每个单元格值均已根据所应用的相应上限进行修整。
相关用法
- Python pandas.map()用法及代码示例
- Python Pandas Series.str.len()用法及代码示例
- Python Pandas.factorize()用法及代码示例
- Python Pandas TimedeltaIndex.name用法及代码示例
- Python Pandas dataframe.ne()用法及代码示例
- Python Pandas Series.between()用法及代码示例
- Python Pandas DataFrame.where()用法及代码示例
- Python Pandas Series.add()用法及代码示例
- Python Pandas.pivot_table()用法及代码示例
- Python Pandas Series.mod()用法及代码示例
- Python Pandas Dataframe.at[ ]用法及代码示例
- Python Pandas Dataframe.iat[ ]用法及代码示例
- Python Pandas.pivot()用法及代码示例
- Python Pandas dataframe.mul()用法及代码示例
- Python Pandas.melt()用法及代码示例
注:本文由纯净天空筛选整理自Shubham__Ranjan大神的英文原创作品 Python | Pandas dataframe.clip_upper()。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
