0

I have a data frame with the below columns, some of the IGFRESAS columns have data in them, but I'll only be replacing the data that doesn't exist based on these conditions - see print screen enter image description here I want to use the condition EV_EM == 0 then copy EV_RND to all the IGFREAS4X columns.

I got this to work for one column:

df["IGFREAS41"]=np.where(df['EV_EM'] == 0, df['EV_RND'], df["IGFREAS41"])

I tried this: columns = ["IGFREAS41", "IGFREAS43", "IGFREAS44", "IGFREAS42"] np.where(df['EV_EM'] == 0, df['EV_RND'], df[columns])

I got this error: ---> 13 np.where(df['EV_EM'] == 0, df['EV_RND'], df[columns])

File <array_function internals>:180, in where(*args, **kwargs)

ValueError: operands could not be broadcast together with shapes (13,) (13,) (13,4)

  • 2
    What's the desired output here? Screenshots aren't very useful for people wanting to run and attempt a solution with your code. Please take a look at https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples as to how to provide data. – Jon Clements Feb 12 '23 at 17:20

1 Answers1

1

if I understand correctly the problem then the following works:

import pandas as pd

data={"EV_EM":[0,1,0,0,1,0,0,1,1],
      "EV_RND":["EM Not Avaiable","","EM Not Avaiable1","EM Not Avaiable2","","EM Not Avaiable3","EM Not Avaiable4","",""],
      "IGFREAS41":["","","","","","","","",""],
      "IGFREAS42":["","","","","","","","",""],
      "IGFREAS43":["","","","","","","","",""],
      }
df=pd.DataFrame(data)


mask=df["EV_EM"]==0

cols_to_fill=[x for x in df.columns if x.startswith("IGFREAS")]

df.loc[mask,cols_to_fill]=df["EV_RND"]
Atanas Atanasov
  • 359
  • 1
  • 10