1

channel_paths['path'] = np.where(
    channel_paths['conversion'] == 0,
    ['Start'] + channel_paths['channel'] + ['Null'],
    ['Start'] + channel_paths['channel'] + ['Conversion'])

I would like to add string Null at the end if conversion is 0 and add conversion column value if conversion value is 1 .

`I have a below error

TypeError: Cannot broadcast np.ndarray with operand of type <class 'list'>

2
  • Remove the brackets around 'Start', 'Null', and 'Conversion' to use strings instead of lists Commented Jun 14, 2021 at 18:25
  • See below error with those changes TypeError: can only concatenate str (not "list") to str Commented Jun 14, 2021 at 18:37

1 Answer 1

1

If you have dataframe like this:

  channel  conversion
0     xxx           0
1     yyy           1

Then this will create new "path" column:

df["path"] = np.where(
    df["conversion"] == 0,
    df["channel"] + "Null",
    df["channel"] + df["conversion"].astype(str),
)

print(df)

Prints:

  channel  conversion     path
0     xxx           0  xxxNull
1     yyy           1     yyy1

EDIT: To append to a list in column "channel":

df.apply(lambda x:  x['channel'].append('Null') if x['conversion'] == 0 else x['channel'].append(x['conversion']), axis=1)
print(df)

Prints:

       channel  conversion
0  [xxx, Null]           0
1     [yyy, 1]           1
Sign up to request clarification or add additional context in comments.

1 Comment

But my channel is a list of string which is causing issue

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.