JavaScript
x
5
1
channel_paths['path'] = np.where(
2
channel_paths['conversion'] == 0,
3
['Start'] + channel_paths['channel'] + ['Null'],
4
['Start'] + channel_paths['channel'] + ['Conversion'])
5
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:
JavaScript
1
4
1
channel conversion
2
0 xxx 0
3
1 yyy 1
4
Then this will create new “path” column:
JavaScript
1
8
1
df["path"] = np.where(
2
df["conversion"] == 0,
3
df["channel"] + "Null",
4
df["channel"] + df["conversion"].astype(str),
5
)
6
7
print(df)
8
Prints:
JavaScript
1
4
1
channel conversion path
2
0 xxx 0 xxxNull
3
1 yyy 1 yyy1
4
EDIT: To append to a list in column “channel”:
JavaScript
1
3
1
df.apply(lambda x: x['channel'].append('Null') if x['conversion'] == 0 else x['channel'].append(x['conversion']), axis=1)
2
print(df)
3
Prints:
JavaScript
1
4
1
channel conversion
2
0 [xxx, Null] 0
3
1 [yyy, 1] 1
4