简体   繁体   中英

Pandas-Update previous and next rows value based on current row value group by Id

I have dataframe, df and want to change the value in column 'Status' for each 'Id'

The rule is: If 'Status' == 'High' update rows before to 'Before' Else 'After'

The dataframe, df:

     Id     Status    
0    1      Low
1    1      Low
2    1      High
3    1      Low
4    2      Low
5    2      Low
6    2      High
7    2      Low
8    3      Low
9    3      Low
10   3      High
11   3      Low
12   3      High
13   3      Low

My expected df:

     Id     Status
 0   1      Before
 1   1      Before
 2   1      High
 3   1      After
 4   2      Before
 5   2      Before
 6   2      High
 7   2      After
 8   3      Before
 9   3      Before
10   3      High
11   3      After
12   3      High
13   3      After 

This is my code so far, (I have not added the rule else change to 'After' yet)

df.loc[df.groupby(['Id'])['Status'] == "High", df['Status'].shift(1)] = 'Before'

I got an error:

ValueError: cannot index with vector containing NA / NaN values

Use numpy.select for set after last High per groups to After and all values with no High to Before :

m1 = df['Status'].eq('High')
m2 = m1.groupby(df['Id']).cumsum() == 0

df['Status1'] = np.select([m1, m2], ['High', 'Before'], default='After') 
print (df)
    Id Status Status1
0    1    Low  Before
1    1    Low  Before
2    1   High    High
3    1    Low   After
4    2    Low  Before
5    2    Low  Before
6    2   High    High
7    2    Low   After
8    3    Low  Before
9    3    Low  Before
10   3   High    High
11   3    Low   After
12   3   High    High
13   3    Low   After

您可以使用地图功能:

df['Status'] = df['Status'].map({'High': 'After', 'Low': 'Before'})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM