Pandas DataFrame, replace the column value with the value of another column

My boosting_df Pandas DataFrame is as follows:

  sku   boost1 boost2 boost3  boost4
0   a  ffffdfg      a   fggg  replace
1   b      fff    fff    fff  replace
2   c      ddf      b    ddf  replace
3   d     dfgd   dfgd    d    replace

for each line, if the "sku" value matches the value of boost1 or boost2 or boost3, then I want to replace the match value with the value inside boost4

Example:

  sku   boost1 boost2 boost3
0   a  ffffdfg replace   fggg
1   b      fff    fff    fff
2   c      ddf      b    ddf
3   d     dfgd   dfgd    replace

I tried:

boosting_df.loc[boosting_df['boost1'] == boosting_df['sku'], 'boost1'] = boosting_df['boost4']
boosting_df.loc[boosting_df['boost2'] == boosting_df['sku'], 'boost2'] = boosting_df['boost4']
boosting_df.loc[boosting_df['boost2'] == boosting_df['sku'], 'boost3'] = boosting_df['boost4']

But I got the following error:

ValueError: shape mismatch: value array of shape (4,) could not be broadcast to indexing result of shape (0,)
+4
source share
1 answer

Each Boolean mask must be applied to both sides of the job, so the size and index will correspond to:

for i in range(1, 4):
    col = 'boost{}'.format(i)
    mask = (df['sku'] == df[col])
    df.loc[mask, col] = df.loc[mask, 'boost4']

Conclusion:

  sku   boost1   boost2   boost3   boost4
0   a  ffffdfg  replace     fggg  replace
1   b      fff      fff      fff  replace
2   c      ddf        b      ddf  replace
3   d     dfgd     dfgd  replace  replace
+3
source

Source: https://habr.com/ru/post/1570827/


All Articles