Python 使用if根据值添加列值

Python 使用if根据值添加列值,python,if-statement,Python,If Statement,我想创建以下数据帧: 我拥有的是A列,根据这些值,我想设置Step_ID列中的值 步骤ID-它从步骤1开始。然后,如果数值较大,则对所有大于0的数值执行步骤2,直到达到零值。然后,应将第3步赋值为零,依此类推 # add a Step ID df = pd.DataFrame({ 'A': ['0','0','0','8.020833015','8.009259224','8.003472328','8.020833015','0','0','5','4.994213104','0','

我想创建以下数据帧:

我拥有的是A列,根据这些值,我想设置Step_ID列中的值

步骤ID-它从步骤1开始。然后,如果数值较大,则对所有大于0的数值执行步骤2,直到达到零值。然后,应将第3步赋值为零,依此类推

# add a Step ID
df = pd.DataFrame({
    'A': ['0','0','0','8.020833015','8.009259224','8.003472328','8.020833015','0','0','5','4.994213104','0','0','0','8.012152672','8.009259224','0']})
step = 0
value = None
def get_step(x):
    global step
    global value
    if x != value:
        value = x
        step += 1
    return f'Step_{step}'
df['Step_ID'] = df['A'].apply(get_step)
df.to_csv('test.csv' , index=None)

上面的代码做了类似的事情,但只使用唯一的数字。要执行所需的功能,是否还需要一个if-if值>0?

试试这个。您可以将阈值调整为所需的值

df = pd.DataFrame({'A': ['0','0','0','8.020833015','8.009259224','8.003472328','8.020833015','0','0','5','4.994213104','0','0','0','8.012152672','8.009259224','0']})

df['A'] = df['A'].astype(float)
diff = df['A']-df['A'].shift().fillna(0)
threshold = 0.1
df['Step_ID'] = (abs(diff)>threshold).cumsum().add(1)
df['Step_ID'] =  'Step_' + df['Step_ID'].astype(str)
df
           A Step_ID
0   0.000000  Step_1
1   0.000000  Step_1
2   0.000000  Step_1
3   8.020833  Step_2
4   8.009259  Step_2
5   8.003472  Step_2
6   8.020833  Step_2
7   0.000000  Step_3
8   0.000000  Step_3
9   5.000000  Step_4
10  4.994213  Step_4
11  0.000000  Step_5
12  0.000000  Step_5
13  0.000000  Step_5
14  8.012153  Step_6
15  8.009259  Step_6
16  0.000000  Step_7

试试这个。您可以将阈值调整为所需的值

df = pd.DataFrame({'A': ['0','0','0','8.020833015','8.009259224','8.003472328','8.020833015','0','0','5','4.994213104','0','0','0','8.012152672','8.009259224','0']})

df['A'] = df['A'].astype(float)
diff = df['A']-df['A'].shift().fillna(0)
threshold = 0.1
df['Step_ID'] = (abs(diff)>threshold).cumsum().add(1)
df['Step_ID'] =  'Step_' + df['Step_ID'].astype(str)
df
           A Step_ID
0   0.000000  Step_1
1   0.000000  Step_1
2   0.000000  Step_1
3   8.020833  Step_2
4   8.009259  Step_2
5   8.003472  Step_2
6   8.020833  Step_2
7   0.000000  Step_3
8   0.000000  Step_3
9   5.000000  Step_4
10  4.994213  Step_4
11  0.000000  Step_5
12  0.000000  Step_5
13  0.000000  Step_5
14  8.012153  Step_6
15  8.009259  Step_6
16  0.000000  Step_7

我可以看到你们实现了异或门,但我们需要一些定制,我已经添加了一个新的功能来检查

import pandas as pd

df = pd.DataFrame({
    'A': ['0','0','0','8.020833015','8.009259224','8.003472328','8.020833015','0','0','5','4.994213104','0','0','0','8.012152672','8.009259224','0']})
step = 0
value = None

def check(x, y):
    try:
        x = float(x)
        y = float(y)
        if x== 0 and y == 0:
            return 0
        elif x == 0 and y > 0:
            return 1
        elif x > 0 and y == 0:
            return 1
        else:
            return 0
    except:
        return 1

def get_step(x):
    global step
    global value
    # if x != value:
    if check(x, value):
        step += 1
        value = x
    return f'Step_{step}'
df['Step_ID'] = df['A'].apply(get_step)
df.to_csv('GSH0211.csv' , index=None)

我可以看到你们实现了异或门,但我们需要一些定制,我已经添加了一个新的功能来检查

import pandas as pd

df = pd.DataFrame({
    'A': ['0','0','0','8.020833015','8.009259224','8.003472328','8.020833015','0','0','5','4.994213104','0','0','0','8.012152672','8.009259224','0']})
step = 0
value = None

def check(x, y):
    try:
        x = float(x)
        y = float(y)
        if x== 0 and y == 0:
            return 0
        elif x == 0 and y > 0:
            return 1
        elif x > 0 and y == 0:
            return 1
        else:
            return 0
    except:
        return 1

def get_step(x):
    global step
    global value
    # if x != value:
    if check(x, value):
        step += 1
        value = x
    return f'Step_{step}'
df['Step_ID'] = df['A'].apply(get_step)
df.to_csv('GSH0211.csv' , index=None)

哇!非常感谢。非常感谢你