Skip to content
Advertisement

Create a new column in a dataframe containing string values from different columns based on values in another column of a dataframe

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’>

Advertisement

Answer

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
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement