Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH/BUG: broadcast a lower-dim assignment if possible #5206

Closed
jreback opened this issue Oct 13, 2013 · 0 comments · Fixed by #5210
Closed

ENH/BUG: broadcast a lower-dim assignment if possible #5206

jreback opened this issue Oct 13, 2013 · 0 comments · Fixed by #5210
Assignees
Labels
Algos Non-arithmetic algos: value_counts, factorize, sorting, isin, clip, shift, diff API Design Indexing Related to indexing on series/frames, not to indexes themselves
Milestone

Comments

@jreback
Copy link
Contributor

jreback commented Oct 13, 2013

http://stackoverflow.com/questions/19346033/how-to-collectively-set-the-values-of-multiple-columns-for-certain-selected-rows/19346925#19346925

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']

This works, but a tad cumbersome

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Algos Non-arithmetic algos: value_counts, factorize, sorting, isin, clip, shift, diff API Design Indexing Related to indexing on series/frames, not to indexes themselves
Projects
None yet
1 participant