Python Pandas Dataframe:规范化0.01和0.99之间的数据吗? [英] Python Pandas Dataframe: Normalize data between 0.01 and 0.99?

查看:115
本文介绍了Python Pandas Dataframe:规范化0.01和0.99之间的数据吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将数据框中的每个值限制在0.01到0.99之间

I am trying to bound every value in a dataframe between 0.01 and 0.99

我已成功使用以下方法对0到1之间的数据进行了规范化: .apply(lambda x:(x-x.min())/(x.max()-x.min()))如下:

I have successfully normalised the data between 0 and 1 using: .apply(lambda x: (x - x.min()) / (x.max() - x.min())) as follows:

df = pd.DataFrame({'one' : ['AAL', 'AAL', 'AAPL', 'AAPL'], 'two' : [1, 1, 5, 5], 'three' : [4,4,2,2]})

df[['two', 'three']].apply(lambda x: (x - x.min()) / (x.max() - x.min()))

df

现在我想将所有值限制在0.01到0.99

Now I want to bound all values between 0.01 and 0.99

这是我尝试过的:

def bound_x(x):
    if x == 1:
        return x - 0.01
    elif x < 0.99:
        return x + 0.01

df[['two', 'three']].apply(bound_x)


df

但是我收到以下错误:

ValueError: ('The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().', u'occurred at index two')


推荐答案

有一个应用,错误 clip方法,为此:

There's an app, err clip method, for that:

import pandas as pd
df = pd.DataFrame({'one' : ['AAL', 'AAL', 'AAPL', 'AAPL'], 'two' : [1, 1, 5, 5], 'three' : [4,4,2,2]})    
df = df[['two', 'three']].apply(lambda x: (x - x.min()) / (x.max() - x.min()))
df = df.clip(lower=0.01, upper=0.99)

收益

    two  three
0  0.01   0.99
1  0.01   0.99
2  0.99   0.01
3  0.99   0.01







The problem with

df[['two', 'three']].apply(bound_x)

bound_x 通过了类似 df ['two']的系列,然后,如果x == 1 要求评估 x == 1 布尔上下文 x == 1 是布尔级数,例如

is that bound_x gets passed a Series like df['two'] and then if x == 1 requires x == 1 be evaluated in a boolean context. x == 1 is a boolean Series like

In [44]: df['two'] == 1
Out[44]: 
0    False
1    False
2     True
3     True
Name: two, dtype: bool

Python尝试将此Series简化为单个布尔值, True False 。熊猫遵循提高的NumPy约定尝试将系列(或数组)转换为布尔值时出现错误

Python tries to reduce this Series to a single boolean value, True or False. Pandas follows the NumPy convention of raising an error when you try to convert a Series (or array) to a bool.

这篇关于Python Pandas Dataframe:规范化0.01和0.99之间的数据吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆