@@ -507,57 +507,18 @@ of one argument to be called on the ``DataFrame``. A *copy* of the original
507
507
DataFrame is returned, with the new values inserted.
508
508
509
509
.. warning ::
510
- Starting from Python 3.6 ``**kwargs `` is an ordered dictionary and `` assign ``
511
- respects the order of the keyword arguments. It is allowed to write
510
+ Starting from Python 3.6 ``**kwargs `` is an ordered dictionary and :func`DataFrame. assign`
511
+ respects the order of the keyword arguments. You may now use assign in the following way:
512
512
513
- .. ipython ::
514
- :verbatim:
513
+ .. ipython :: python
515
514
516
- In [1]: # Allowed for Python 3.6 and later
517
- df.assign(C = lambda x: x['A'] + x['B'],
518
- D = lambda x: x['A'] + x['C'])
515
+ In [1 ]: df.assign(C = lambda x : x[' A' ] + x[' B' ],
516
+ D = lambda x : x[' A' ] + x[' C' ])
519
517
520
518
This may subtly change the behavior of your code when you're
521
519
using ``.assign() `` to update an existing column. Prior to Python 3.6,
522
520
callables referring to other variables being updated would get the "old" values
523
521
524
- Previous Behaviour:
525
-
526
- .. code-block :: ipython
527
-
528
- In [2]: df = pd.DataFrame({"A": [1, 2, 3]})
529
-
530
- In [3]: df.assign(A=lambda df: df.A + 1, C=lambda df: df.A * -1)
531
- Out[3]:
532
- A C
533
- 0 2 -1
534
- 1 3 -2
535
- 2 4 -3
536
-
537
- New Behaviour:
538
-
539
- .. ipython :: python
540
-
541
- df.assign(A = df.A+ 1 , C = lambda df : df.A* - 1 )
542
-
543
- For Python 3.5 and earlier the function signature of ``assign `` is ``**kwargs ``,
544
- a dictionary, the order of the new columns in the resulting DataFrame cannot be guaranteed
545
- to match the order you pass in. To make things predictable, items are inserted
546
- alphabetically (by key) at the end of the DataFrame.
547
-
548
- All expressions are computed first, and then assigned. So you can't refer
549
- to another column being assigned in the same call to ``assign ``. For example:
550
-
551
- .. ipython ::
552
- :verbatim:
553
-
554
- In [1]: # Don't do this, bad reference to `C `
555
- df.assign(C = lambda x: x['A'] + x['B'],
556
- D = lambda x: x['A'] + x['C'])
557
- In [2]: # Instead, break it into two assigns
558
- (df.assign(C = lambda x: x['A'] + x['B'])
559
- .assign(D = lambda x: x['A'] + x['C']))
560
-
561
522
Indexing / Selection
562
523
~~~~~~~~~~~~~~~~~~~~
563
524
The basics of indexing are as follows:
0 commit comments