Jui Sen
Jui Sen

Reputation: 377

replacing the value of one column conditional on two other columns in pandas

I have a data-frame df:

year ID  category 
1     1    0        
2     1    1        
3     1    1        
4     1    0        
1     2    0        
2     2    0        
3     2    1        
4     2    0        

I want to create a new column such that: for a particular 'year' if the 'category' is 1, the 'new-category' will be always 1 for the upcoming years:

year ID  category new_category
1     1    0        0
2     1    1        1
3     1    1        1
4     1    0        1
1     2    0        0
2     2    0        0
3     2    1        1
4     2    0        1

I have tried if-else condition but I am getting the same 'category' column

for row in range(1,df.category[i-1]):
    df['new_category'] = df['category'].replace('0',df['category'].shift(1))

But I am not getting the desired column

Upvotes: 1

Views: 67

Answers (1)

Nk03
Nk03

Reputation: 14949

TRY:

df['new_category'] = df.groupby('ID')['category'].cummax()

OUTPUT:

   year  ID  category  new_category
0     1   1         0             0
1     2   1         1             1
2     3   1         1             1
3     4   1         0             1
4     1   2         0             0
5     2   2         0             0
6     3   2         1             1
7     4   2         0             1

Upvotes: 4

Related Questions