Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.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,如果当前值高于上一行的值,我希望将数据标记为“1”,否则标记为“0” 假设我有这个数据帧: df = pd.DataFrame({'date': [1,2,3,4,5], 'price': [50.125, 45.25, 65.857, 100.956, 77.4152]}) 我想要输出,就像数据帧是这样构造的: df = pd.DataFrame({'date': [1,2,3,4,5], 'price': [50.125, 45.25, 65.857, 100.956, 77.4152],

如果当前值高于上一行的值,我希望将数据标记为“1”,否则标记为“0”

假设我有这个数据帧:

df = pd.DataFrame({'date': [1,2,3,4,5], 'price': [50.125, 45.25, 65.857, 100.956, 77.4152]})
我想要输出,就像数据帧是这样构造的:

df = pd.DataFrame({'date': [1,2,3,4,5], 'price': [50.125, 45.25, 65.857, 100.956, 77.4152], 'label':[0, 0, 1, 1, 0]})
*我不知道如何发布数据帧

以下代码是我的尝试:

df['label'] = 0
i = 0
for price in df['price']:
    i = i+1
    if price in i > price: #---> right now I am stuck here. i=It says argument of type 'int' is not iterable
        df.append['label', 1]
    elif price in i <= price:
        df.append['label', 0]
df['label']=0
i=0
对于df中的价格[‘价格’]:
i=i+1
如果价格在i>price:#-->现在我被困在这里。i=它表示“int”类型的参数不可iterable
df.append['label',1]

iIIUC
np.中的elif价格,其中
使用布尔值
shift
查看行价格的变化,以及它是否大于上面的行

df['label'] = np.where(df['price'].ge(df['price'].shift()),1,0)

print(df)

   date     price  label
0     1   50.1250      0
1     2   45.2500      0
2     3   65.8570      1
3     4  100.9560      1
4     5   77.4152      0
说明:

print(df['price'].ge(df['price'].shift()))
返回可在
where
子句中使用的
True
False
语句的布尔值

0    False
1    False
2     True
3     True
4    False
通过(
=
)创建布尔掩码,并通过以下方式将
真/假
映射为
1/0
的整数:

或通过:


要解释代码中发生了什么,请执行以下操作:

  • df['label']
    应初始化为空列表,而不是“0”。如果要将列表的第一个值设置为0,可以执行
    df['label']=[0]
  • i
    只是指数值(0,1,2,3…),而不是特定指数(50.125,45.25,65.857…)的价格值,因此它不是您想要比较的
  • price in
    用于检查以下列表中是否存在price变量的值。
语句中的
后面没有列表,因此会出现错误。相反,您希望获取特定指数的价格值,并比较其是否大于或小于上一个指数的值
  • append
    方法使用
    ()
    而不是
    []
  • 如果要继续使用循环的方法,可以使用以下方法:

    df['label'] = []
    for i in range(len(df['price'])):
        if df['price'][i] > df['price'][i - 1]:
            df['label'].append(1)
        else:
            df['label'].append(0)
    
    这样做的目的是在价目表的长度范围内循环。然后比较位置
    i
    和位置
    i-1
    的价格值

    还可以使用三元运算符进一步简化if/else语句,以:

    df['label'].append(1 if df['price'][i] > df['price'][i - 1] else 0)
    
    工作小提琴:

    df['label'] = []
    for i in range(len(df['price'])):
        if df['price'][i] > df['price'][i - 1]:
            df['label'].append(1)
        else:
            df['label'].append(0)
    
    df['label'].append(1 if df['price'][i] > df['price'][i - 1] else 0)