Ali
Ali

Reputation: 113

Update value in data frame when condition meets

I have a dataframe which looks as follows: df = pd.DataFrame({'Item': ['A', 'A', 'A', 'A', 'A', 'A', 'B', 'B', 'C', 'C', 'D','D','D'],'Item Flag': [0, 0, 0, 1, 0, 0, 1,0, 0, 0, 0, 1, 1]})

I want to update the value of item flag column. The logic is if there is an item flagged as 1, then the following same item will be flagged as 1 but the same items before won't change.

The ideal output table looks like this: df = pd.DataFrame({'Item': ['A', 'A', 'A', 'A', 'A', 'A', 'B', 'B', 'C', 'C', 'D','D','D'],'Item Flag': [0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1]})

Upvotes: 0

Views: 70

Answers (4)

wwnde
wwnde

Reputation: 26676

Use pandas where to mask zeroes and then broadcast 1 using group by.

df['Item Flag']=df.where(df['Item Flag']==1).groupby(df['Item'])['Item Flag'].ffill().fillna(0)

Item  Item Flag
0     A        0.0
1     A        0.0
2     A        0.0
3     A        1.0
4     A        1.0
5     A        1.0
6     B        1.0
7     B        1.0
8     C        0.0
9     C        0.0
10    D        0.0
11    D        1.0
12    D        1.0

Upvotes: 0

Quixotic22
Quixotic22

Reputation: 2924

The cumulative max is probably the neatest solution

df['Item Flag'] = df.groupby('Item')['Item Flag'].cummax()

   Item  Item Flag
0     A          0
1     A          0
2     A          0
3     A          1
4     A          1
5     A          1
6     B          1
7     B          1
8     C          0
9     C          0
10    D          0
11    D          1
12    D          1

Upvotes: 1

user17242583
user17242583

Reputation:

You can replace zeros with NaN, group by Item, forward fill, and change back remaining NaNs:

df['Item Flag'] = df['Item Flag'].replace(0, np.nan).groupby(df['Item']).ffill().fillna(0).astype(int)

Output:

>>> df
Item  Item Flag
0     A          0
1     A          0
2     A          0
3     A          1
4     A          1
5     A          1
6     B          1
7     B          1
8     C          0
9     C          0
10    D          0
11    D          1
12    D          1

Upvotes: 1

BENY
BENY

Reputation: 323226

Try with groupby + cummax

df['Item Flag'] = df.groupby(['Item'])['Item Flag'].cummax()
df
Out[21]: 
   Item  Item Flag
0     A          0
1     A          0
2     A          0
3     A          1
4     A          1
5     A          1
6     B          1
7     B          1
8     C          0
9     C          0
10    D          0
11    D          1
12    D          1

Upvotes: 3

Related Questions