我想在python中为值范围内的变量赋值

问题描述 投票:0回答:1

我想为我的变量赋值,以便它应包含大于特定值的所有值。我正在使用np.range但是我们还需要将参数传递为np.range(start_range, end_range, Difference between two values)。我希望我的变量包含大于(start_range)的所有值

我的数据框包含列分数范围0-1。假设我创建了一个函数如下:

def get_data(Data, score):
    ......
    ......

现在,当我调用此函数时,我希望我的Dataframe中得分超过0.8的所有记录。

有没有其他方法来分配范围内的值?

python python-3.x numpy
1个回答
2
投票

我想你想要从数据帧中返回大于阈值(0.8)的所有值。 range不是这个的工具。

def get_data(df, score, column):
    """Return rows of Dataframe where 'column' has value greater than 'score'."""
    new_df = df.loc[df[column] > score]
    return new_df

>>> get_data(df, 0.8, 'Score')
© www.soinside.com 2019 - 2024. All rights reserved.