如何在Python中找到增减趋势

用户名

我正在尝试逐行比较数据框的不同列

for (i= startday to endday)
    if(df[i]<df[i+1])
    counter=counter+1
    else
    i=endday+1

目标是发现增加(或减少)趋势(需要连续),而我的数据如下所示

df= 1 2 3 0 1 1 1
    1 1 1 1 0 1 2
    1 2 1 0 1 1 2 
    0 0 0 0 1 0 1 

(在此示例中,开始日期到结束日期为7,但实际上这两个是不稳定的)

结果,我希望找到这个{2,0,1,0},并且我需要它能够快速运行,因为我的数据很大(120万)。由于时间限制,我尝试不使用循环(例如,如果使用循环等)

我尝试了下面的代码,但如果条件为假,则找不到如何停止计数器

import math
import numpy as np
import pandas as pd

df1=df.copy()   
df2=df.copy()
bool1 = (np.less_equal.outer(startday.startday, range(1,13)) 
            & np.greater_equal.outer(endday.endday, range(1,13))
            )

bool1= np.c_[np.zeros(len(startday)),bool1].astype('bool')

bool2 = (np.less_equal.outer(startday2.startday2, range(1,13)) 
            & np.greater_equal.outer(endday2.endday2, range(1,13))
            )

bool2= np.c_[bool2, np.zeros(len(startday))].astype('bool')


df1.insert(0, 'c_False',math.pi)
df2.insert(12, 'c_False',math.pi)
#df2.head()
arr_bool = (bool1&bool2&(df1.values<df2.values))

df_new = pd.DataFrame(np.sum(arr_bool , axis=1), 
                        index=data_idx, columns=['coll'])

df_new.coll= np.select( condlist = [startday.startday > endday.endday],
                         choicelist = [-999], 
                         default = df_new.coll)
Mstaino

在末尾添加零,然后使用,然后使用以下方法np.diff获得第一个“非正数” argmin

(np.diff(np.hstack((df.values, np.zeros((df.values.shape[0], 1)))), axis=1) > 0).argmin(axis=1)
>> array([2, 0, 1, 0], dtype=int64)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章