0

I have the following df:

ICB sICB
A B1
ZB1 ZC1
ZB1 ZC2
ZB1 ZC3

For each row, I want to replace ZB1 with the value in the column sICB.

All other related answers on SO are referring to "some other value" as part of a condition. But I need to replace any occurrence of ZB1 with whatever is in the sICB column. There must be a simple way to do it, but I can't come up with it.

I've tried with df[loc] with DataFrame.replace(), mask, where.

What intuitively seems correct is:

df.where([df['ICB'].eq('ZB1'), df[sICB])

  • `df.loc[df['ICB'], eq('ZB1'), 'ICB'] = df['sICB']` or `m = df['ICB'], eq('ZB1') ; df.loc[m, 'ICB'] = df.loc[m, 'sICB']` – mozway Jun 29 '23 at 12:29
  • Can also do `df['ICB'] = np.where([df['ICB'].eq('ZB1'), df[sICB], df['ICB'])` – Tyler Jun 29 '23 at 14:45

0 Answers0