File tree Expand file tree Collapse file tree 1 file changed +18
-3
lines changed Expand file tree Collapse file tree 1 file changed +18
-3
lines changed Original file line number Diff line number Diff line change @@ -518,9 +518,24 @@ respects the order of the keyword arguments. You can use assign in the following
518518
519519 .. warning ::
520520
521- This may subtly change the behavior of your code when you are
522- using :func: `DataFrame.assign ` to update an existing column. Prior to Python 3.6,
523- callables referring to other variables being updated would get the "old" values.
521+ Prior to Python 3.6, this may subtly change the behavior of your code when you are
522+ using :func: `DataFrame.assign ` to update an existing column.
523+
524+ Since the function signature of ``assign `` is ``**kwargs ``, a dictionary,
525+ the order of the new columns in the resulting DataFrame cannot be guaranteed
526+ to match the order you pass in. To make things predictable, items are inserted
527+ alphabetically (by key) at the end of the DataFrame.
528+
529+ .. ipython ::
530+ :verbatim:
531+
532+ In [1]: # Don't do this, bad reference to `C `
533+ df.assign(C = lambda x: x['A'] + x['B'],
534+ D = lambda x: x['A'] + x['C'])
535+ In [2]: # Instead, break it into two assigns
536+ (df.assign(C = lambda x: x['A'] + x['B'])
537+ .assign(D = lambda x: x['A'] + x['C']))
538+
524539
525540Indexing / Selection
526541~~~~~~~~~~~~~~~~~~~~
You can’t perform that action at this time.
0 commit comments