简体   繁体   中英

Assigning values to multiple columns based on a single condition using Pandas (Python)

I have to create multiple new columns based on a single condition. I have a code that has multiple lines, is there a way to shorten or optimize the below code, since I have to repeat it numerous times:

LOOKUP['Publisher']= np.where(LOOKUP['Subchannel'].str.contains("PROMO"),"Instagram",LOOKUP['Publisher'])
LOOKUP['Channel']= np.where(LOOKUP['Subchannel'].str.contains("PROMO"),"Social",LOOKUP['Channel'])
LOOKUP['Source']=  np.where(LOOKUP['Subchannel'].str.contains("PROMO"),"AA",LOOKUP['Source'])

If the column doesn't exist already, you can try the following to avoid multiple where statements:

condition = LOOKUP['Subchannel'].str.contains("PROMO")
# for all those rows where condition is not satisfied, insert a blank
LOOKUP[['Publisher','Channel','Source']] = 
pd.DataFrame(np.where(condition[:, None], ['Instagram','Social','AA'], ['','','']))

The question is a bit confusing because it looks like you already have the columns you want to create. Are they pre-initialised with empty data or something similar?

I think what the other people is telling you is something like this:

cols = ['Publisher','Channel','Source', ...] # Name of the columns you want to iterate
values = ['Instagram', 'Social', 'AA', ...] # The values you want to yield after np.where()

for i in range(len(cols)):
    LOOKUP[cols[i]] = np.where(LOOKUP['Subchannel'].str.contains("PROMO"),values[i], LOOKUP[cols[i]])

This way you don't need to write many similar lines. Just update cols and values instead.

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