求最小值的应用

How to calculate df.cummin() piecewise

I want to start df.cummin() when cond column is true until recompute df.cummin() next cond column is true.

The result is assigned to expected column

input

import pandas as pd
import numpy as np
A=[17,18,21,15,18,19,22,16,30,50,]
cond=[False,True,False,False,False,True,False,False,True,False]
df=pd.DataFrame({'A':A,'cond':cond})
df

Answers

You're looking to groupby the cumsum of the cond column. Since you don't want any values up until the first True, you need to essentially delete values for group zero.

import pandas as pd
import numpy as np
A=[17,18,21,15,18,19,22,16,30,50,]
cond=[False,True,False,False,False,True,False,False,True,False]
df=pd.DataFrame({'A':A,'cond':cond})


df['expected'] = df.groupby(df.cond.cumsum())['A'].cummin()
df.loc[df.cond.cumsum().eq(0), 'expected'] = np.nan
    A   cond  expected
0  17  False       NaN
1  18   True      18.0
2  21  False      18.0
3  15  False      15.0
4  18  False      15.0
5  19   True      19.0
6  22  False      19.0
7  16  False      16.0
8  30   True      30.0
9  50  False      30.0

How to calculate with cummin() until condition is true

I want to calculate the minimum value until cond column is true

Then recalculate the minimum value starting from the next row where the cond column is true

The obtained result is assigned to the expected column

input

import pandas as pd
import numpy as np
A=[16,12,21,15,18,19,13,16,10,50]
cond=[False,False,True,False,False,True,False,False,True,False]
df=pd.DataFrame({'A':A,'cond':cond})
df

Answers

Calculate the reverse cumsum on cond to identify blocks of rows, then group the column A by these blocks and transform with min to calculate minimum value per block then mask the values and use ffill to propagate last min values in forward direction

b = df['cond'][::-1].cumsum()
df['result'] = df['A'].groupby(b).transform('min').mask(~df['cond']).ffill()
 A   cond  result
0  16  False     NaN
1  12  False     NaN
2  21   True    12.0
3  15  False    12.0
4  18  False    12.0
5  19   True    15.0
6  13  False    15.0
7  16  False    15.0
8  10   True    10.0
9  50  False    10.0

How to get the minimum value between 2 conditions

Starts when the start column is True and ends when the end column is True

Input:

import pandas as pd
import numpy as np
A = [6, 12, 21, 15, 18, 19, 13, 9, 10, 50]
cond1 = [False, True, False, False, False, False, True, False, False, False]
cond2 = [False, False, False, True, False, False, False, False, True, False]
df = pd.DataFrame({'A' : A, 'start' : cond1, 'end' : cond2})

Answers

We may need to create the groupby key with cumsum , then transform the min of each group and shift it

df.loc[df.end,'new'] = (df.groupby([df['start'].cumsum(),df['end'].cumsum()]).
                        A.transform('min').shift())
df['new'] = df['new'].ffill()
df
Out[333]: 
    A  start    end   new
0   6  False  False   NaN
1  12   True  False   NaN
2  21  False  False   NaN
3  15  False   True  12.0
4  18  False  False  12.0
5  19  False  False  12.0
6  13   True  False  12.0
7   9  False  False  12.0
8  10  False   True   9.0
9  50  False  False   9.0

 

posted @ 2022-07-17 20:31  C羽言  阅读(25)  评论(0)    收藏  举报