Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update row values in a dataframe based on another row's values?

I have a dataframe with two columns: a and b

df

         a         b
0       john      123
1       john
2       mark     
3       mark      456
4       marcus    789

I want to update values of b column based on a column.

         a         b
0       john      123
1       john      123
2       mark      456
3       mark      456
4       marcus    789

If john has value 123 in b. Remaining john also must have same value.


1 Answers

Assuming your dataframe is:

df = pd.DataFrame({'a': ['john', 'john', 'mark', 'mark', 'marcus'], 'b': [123, '', '', 456, 789]})

You can df.groupby the dataframe on column a and then apply transform on the column b of the grouped dataframe returning the first non empty value in the grouped column b.

Use:

df['b'] = (
    df.groupby('a')['b']
    .transform(lambda s: s[s.ne('')].iloc[0] if s.ne('').any() else s)
)

Result:

# print(df)

        a    b
0    john  123
1    john  123
2    mark  456
3    mark  456
4  marcus  789
like image 101
Shubham Sharma Avatar answered Jun 17 '26 15:06

Shubham Sharma



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!