how to find number of consecutive decreases(increases)

后端 未结 1 342
走了就别回头了
走了就别回头了 2021-01-23 07:24

how to find number of consecutive decreases(increases)

I have a dataframe which has 500K rows and 12 columns for months and include start and end month. Every columns re

相关标签:
1条回答
  • 2021-01-23 08:01

    So I think instead of using argmin, you can use idxmin after renaming the column to get an integer value of the position. Then remove the value of the startMonth such as:

    incr = (df1.rename(columns={col:int(col.split('_')[1]) for col in masked.columns})
               .diff(-1, axis=1) < 0).mask(~arr_bool).idxmin(axis=1) - startMonth.start
    decr = (df1.rename(columns={col:int(col.split('_')[1]) for col in masked.columns})
               .diff(-1, axis=1) > 0).mask(~arr_bool).idxmin(axis=1) - startMonth.start
    

    Then you can do the np.select as you do, or probably just a .illna(-999) should be enough as I think with this solution everywhere you have nan in the result will be where your condition startMonth.start > endMonth.end is met

    0 讨论(0)
提交回复
热议问题