0

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'])
4
  • You can use a for loop when your column name is the key, and your where condition is the value. Commented Jul 13, 2020 at 15:09
  • What is LOOKUP here, exactly? Commented Jul 13, 2020 at 15:18
  • 1
    The columns are already created right? Would give an error here otherwise Commented Jul 13, 2020 at 15:19
  • I have lookup as a dataframe. The columns don't pre-exist. Commented Jul 13, 2020 at 17:28

2 Answers 2

1

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'], ['','','']))
Sign up to request clarification or add additional context in comments.

3 Comments

Sorry, I misunderstood you earlier. I want to create new columns. The new columns dont pre-exist. Can you please suggest what would be change to the code?
@Newbie123 did you get a chance to try this?
Sorry, could not acknowledge earlier. The solution worked. Thanks.
0

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.

2 Comments

Sorry for not being very specific. I want to create new columns. The new columns dont pre-exist. Can you please suggest what would be the changes to the code if the columns dont pre=exist.
If the columns don't pre-exist, why are you using them inside np.where()? Maybe you could try this: np.where(LOOKUP['Subchannel'].str.contains("PROMO"),values[i], ''). This will add empty strings for the rows that don't match the condition.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.