3

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.

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks. But if last row's 'b' value Is empty. marcus is ` `. Then what's should be the case
@Zanthoxylumpiperitum Well in that case i have to modify the answer.
@Zanthoxylumpiperitum What value do you expect in column b in such case?
Same as the original value
@Zanthoxylumpiperitum Check the edited answer. Now this should work in such case.
0

Example:

df = pd.DataFrame({'A': [0," ", 2, 3, 4],
                   'B': [5, 6, 7, 8, 9],
                   'C': ['a', 'b', 'c', 'd', 'e']})

df1=df.replace({'A':" "},3)    

Hope this helps, In your case it would be like

df1=df.replace({'b':" "},123)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.