Reputation:
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.
Upvotes: 3
Views: 440
Reputation: 71689
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
Upvotes: 1
Reputation: 39
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)
Upvotes: 0