Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何根据条件将列中的值减半?_Python_Pandas - Fatal编程技术网

Python 如何根据条件将列中的值减半?

Python 如何根据条件将列中的值减半?,python,pandas,Python,Pandas,我希望实现以下业务逻辑:对于列间隙的阈值4,对于ID的第一个实例,当间隙超过此阈值时,我希望将分数减半。不仅如此,还应该相应地更新该ID的后续事务。e、 g.见下表B中的索引2和3。只要以下交易中的差距我想不出一个很好的矢量化解决方案来解决这个问题,那么1000点的差异(即新点)应该添加到新的_点上,因此可能还有改进的余地。解决这个问题的一种方法是使用迭代为每个组构建逻辑,并在运行时跟踪以前的点值和当前的点值 鉴于: ID trn_amt month_of_trn gap old_p

我希望实现以下业务逻辑:对于列
间隙
的阈值4,对于ID的第一个实例,当间隙超过此阈值时,我希望将分数减半。不仅如此,还应该相应地更新该ID的后续事务。e、 g.见下表B中的索引2和3。只要以下交易中的差距我想不出一个很好的矢量化解决方案来解决这个问题,那么1000点的差异(即新点)应该添加到新的_点上,因此可能还有改进的余地。解决这个问题的一种方法是使用迭代为每个组构建逻辑,并在运行时跟踪以前的点值和当前的点值

鉴于:

  ID  trn_amt  month_of_trn  gap  old_points
0  A      100             0  0.0  1000
1  A      140             3  3.0  2000
2  A      210             9  6.0  3000
3  A      320            10  1.0  4000
4  A      580            13  3.0  5000
5  B      101             0  0.0  6000
6  B      120             2  2.0  7000
7  B      300             8  6.0  8000
8  B      200            10  2.0  9000
复制它,然后使用pd.read_剪贴板允许我们加载df

import pandas as pd
df = pd.read_clipboard(sep='\s\s+')
然后,只需构建一个完成工作的函数

def custom_transform(df):
    gaps = df['gap']
    old_points = df['old_points']
    is_gap_big = False #boolean turns True when gap is big enough (>4)
    zipped = zip(gaps, old_points)
    _, prev_point = next(zipped)
    new_points = [prev_point]
    #This loop essentially has access to prev_point, gap, and point. The value for point changes based on conditionals.
    for gap, point in zipped:
        if is_gap_big:
            point = prev_point + 1000
        if gap > 4:
            is_gap_big = True
            point = point // 2 #i am assuming you want ints, and floor operations are fine
        new_points.append(point)
        prev_point = point
    df['new_points'] = new_points
    return df
并应用于数据帧

out = df.groupby('ID').apply(custom_transform)

print(out)
输出:

  ID  trn_amt  month_of_trn  gap  old_points  new_points
0  A      100             0  0.0        1000        1000
1  A      140             3  3.0        2000        2000
2  A      210             9  6.0        3000        1500
3  A      320            10  1.0        4000        2500
4  A      580            13  3.0        5000        3500
5  B      101             0  0.0        6000        6000
6  B      120             2  2.0        7000        7000
7  B      300             8  6.0        8000        4000
8  B      200            10  2.0        9000        5000
out = df.groupby('ID').apply(custom_transform)

print(out)
  ID  trn_amt  month_of_trn  gap  old_points  new_points
0  A      100             0  0.0        1000        1000
1  A      140             3  3.0        2000        2000
2  A      210             9  6.0        3000        1500
3  A      320            10  1.0        4000        2500
4  A      580            13  3.0        5000        3500
5  B      101             0  0.0        6000        6000
6  B      120             2  2.0        7000        7000
7  B      300             8  6.0        8000        4000
8  B      200            10  2.0        9000        5000