CoronaVirus
CoronaVirus

Reputation: 421

Update specific column values based upon group by from different column in Pandas

I have below pandas data frame in python.

enter image description here

Looking for below output:

enter image description here

Based upon group of last column I need to pick and repeat first value from and in middle column.

Upvotes: 0

Views: 59

Answers (1)

Quang Hoang
Quang Hoang

Reputation: 150735

Try:

df['col1'] = df['col1'].mask(df['col2'].duplicated()).ffill()

Or:

df['col1'] = df.groupby('col2')['col1'].transform('first')

Upvotes: 2

Related Questions