ENH/BUG: broadcast a lower-dim assignment if possible · Issue #5206 · pandas-dev/pandas (original) (raw)
This does not work as we are using a rhs of a series which is not broadcast
to the assignment
df.loc[df['A'],['A','B']] = df['C']
In [17]: df = DataFrame(dict(A = [1,2,0,0,0],B=[0,0,0,10,11],C=[3,4,5,6,7]))
In [18]: df
Out[18]:
A B C
0 1 0 3
1 2 0 4
2 0 0 5
3 0 10 6
4 0 11 7
In [19]: mask = df['A'] == 0
In [20]: for col in ['A','B']:
....: df.loc[mask,col] = df['C']
....:
In [21]: df
Out[21]:
A B C
0 1 0 3
1 2 0 4
2 5 5 5
3 6 6 6
4 7 7 7