@@ -2659,8 +2659,11 @@ def assign(self, **kwargs):
2659
2659
\*\*kwargs. For python 3.5 and earlier, since \*\*kwargs is unordered,
2660
2660
the columns are inserted in alphabetical order at the end of your
2661
2661
DataFrame. Assigning multiple columns within the same ``assign``
2662
- is possible, but you cannot reference other columns created within
2663
- the same ``assign`` call.
2662
+ is possible, but for python 3.5 and eralier you cannot reference other
2663
+ columns created within the same ``assign`` call. For python 3.6 and
2664
+ above it is possible to reference columns created in an assignment.
2665
+ To this end you have to respect the order of |*|*kwargs and use
2666
+ callables referencing the assigned columns.
2664
2667
2665
2668
Examples
2666
2669
--------
@@ -2683,7 +2686,7 @@ def assign(self, **kwargs):
2683
2686
2684
2687
Where the value already exists and is inserted:
2685
2688
2686
- >>> newcol = np.log(df['A'])
2689
+ >>> newcol = np.log(df['A']. )
2687
2690
>>> df.assign(ln_A=newcol)
2688
2691
A B ln_A
2689
2692
0 1 0.426905 0.000000
@@ -2699,19 +2702,21 @@ def assign(self, **kwargs):
2699
2702
"""
2700
2703
data = self .copy ()
2701
2704
2702
- # do all calculations first...
2703
- results = OrderedDict ()
2704
- for k , v in kwargs .items ():
2705
- results [k ] = com ._apply_if_callable (v , data )
2706
-
2707
- # preserve order for 3.6 and later, but sort by key for 3.5 and earlier
2705
+ # for 3.6 preserve order of kwargs
2708
2706
if PY36 :
2709
- results = results .items ()
2707
+ for k , v in kwargs .items ():
2708
+ data [k ] = com ._apply_if_callable (v , data )
2710
2709
else :
2711
- results = sorted (results .items ())
2712
- # ... and then assign
2713
- for k , v in results :
2714
- data [k ] = v
2710
+ # for 3.5 or earlier: do all calculations first...
2711
+ results = OrderedDict ()
2712
+ for k , v in kwargs .items ():
2713
+ results [k ] = com ._apply_if_callable (v , data )
2714
+
2715
+ # sort by key for 3.5 and earlier
2716
+ results = sorted (results .items ())
2717
+ # ... and then assign
2718
+ for k , v in results :
2719
+ data [k ] = v
2715
2720
return data
2716
2721
2717
2722
def _sanitize_column (self , key , value , broadcast = True ):
0 commit comments