简体   繁体   中英

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.

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM