@@ -3251,32 +3251,33 @@ def assign(self, **kwargs):
3251
3251
Examples
3252
3252
--------
3253
3253
>>> df = pd.DataFrame({'temp_c': (17.0, 25.0)},
3254
- index=['portland', 'berkeley'])
3254
+ index=['Portland', 'Berkeley'])
3255
+ temp_c
3256
+ Portland 17.0
3257
+ Berkeley 25.0
3255
3258
3256
3259
Where the value is a callable, evaluated on `df`:
3257
-
3258
3260
>>> df.assign(temp_f=lambda x: x.temp_c * 9 / 5 + 32)
3259
3261
temp_c temp_f
3260
- portland 17.0 62.6
3261
- berkeley 25.0 77.0
3262
-
3263
- Where the value already exists and is inserted:
3262
+ Portland 17.0 62.6
3263
+ Berkeley 25.0 77.0
3264
3264
3265
+ Alternatively, the same behavior can be achieved by directly
3266
+ referencing an existing Series or list-like:
3265
3267
>>> newcol = df['temp_c'] * 9 / 5 + 32
3266
3268
>>> df.assign(temp_f=newcol)
3267
3269
temp_c temp_f
3268
- portland 17.0 62.6
3269
- berkeley 25.0 77.0
3270
-
3271
- Where the keyword arguments depend on each other
3272
-
3273
- >>> df = pd.DataFrame({'A': [1, 2, 3]})
3274
-
3275
- >>> df.assign(B=df.A, C=lambda x:x['A']+ x['B'])
3276
- A B C
3277
- 0 1 1 2
3278
- 1 2 2 4
3279
- 2 3 3 6
3270
+ Portland 17.0 62.6
3271
+ Berkeley 25.0 77.0
3272
+
3273
+ In Python 3.6+, you can create multiple columns within the same assign
3274
+ where one of the columns depends on another one defined within the same
3275
+ assign:
3276
+ >>> df.assign(temp_f=lambda x: x['temp_c'] * 9 / 5 + 32,
3277
+ temp_k=lambda x: (x['temp_f'] + 459.67) * 5 / 9)
3278
+ temp_c temp_f temp_k
3279
+ Portland 17.0 62.6 290.15
3280
+ Berkeley 25.0 77.0 298.15
3280
3281
"""
3281
3282
data = self .copy ()
3282
3283
0 commit comments