Skip to content

Commit ef49f88

Browse files
committed
DOC: adjusted docstring examples in DataFrame.assign to illustrate python3.6+ feature.
1 parent de61b38 commit ef49f88

File tree

1 file changed

+19
-18
lines changed

1 file changed

+19
-18
lines changed

pandas/core/frame.py

+19-18
Original file line numberDiff line numberDiff line change
@@ -3251,32 +3251,33 @@ def assign(self, **kwargs):
32513251
Examples
32523252
--------
32533253
>>> 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
32553258
32563259
Where the value is a callable, evaluated on `df`:
3257-
32583260
>>> df.assign(temp_f=lambda x: x.temp_c * 9 / 5 + 32)
32593261
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
32643264
3265+
Alternatively, the same behavior can be achieved by directly
3266+
referencing an existing Series or list-like:
32653267
>>> newcol = df['temp_c'] * 9 / 5 + 32
32663268
>>> df.assign(temp_f=newcol)
32673269
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
32803281
"""
32813282
data = self.copy()
32823283

0 commit comments

Comments
 (0)