I have a Pandas DataFrame which looks like:
Row | Source | Location | Campaign | New Campaign | some other columns |
---|---|---|---|---|---|
1 | s1 | l1 | c1 | nc1 | … |
2 | s2 | l2 | c2 | nc2 | … |
3 | s3 | l3 | c3 | nc3 | … |
and there is a (boolean) condition crit
which is a mix of some rules based on some other columns
. What I want to achieve is:
If the condition crit
is met (for a row):
- Replace
Source
,Location
by constantx
andy
. - Replace
Campaign
byNew Campaign
.
For example if row #1 and #3 match the condition, desired output is:
Row | Source | Location | Campaign | New Campaign | some other columns |
---|---|---|---|---|---|
1 | x | y | nc1 | nc1 | … |
2 | s2 | l2 | c2 | nc2 | … |
3 | x | y | nc3 | nc3 | … |
Currently, I am doing this in 2 steps, firstly
df.loc[crit, ['Source', 'Location']] = ['x', 'y']
then
df.loc[crit, 'Campaign'] = df.loc[crit, 'New Campaign']
But wonder if there is a way to do it in one go. df.loc[crit, ['Source', 'Location', 'Campaign']] = ['x', 'y', df.loc[crit, 'New Campaign']]
throws error.