Skip to content

Commit cb2dba9

Browse files
committed
DOC: simplified examples in DataFrame.assign docstring
1 parent 405c57a commit cb2dba9

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

pandas/core/frame.py

+13-11
Original file line numberDiff line numberDiff line change
@@ -3354,23 +3354,25 @@ def assign(self, **kwargs):
33543354
33553355
Examples
33563356
--------
3357-
>>> df = pd.DataFrame({'A': range(1, 3),
3358-
... 'B': np.arange(-1.0, 2.0, 1.5)})
3357+
>>> df = pd.DataFrame([('or', 17.),('ca', 25)],
3358+
index=['portland', 'berkeley'],
3359+
columns=['state', 'temp_c'])
33593360
33603361
Where the value is a callable, evaluated on `df`:
33613362
3362-
>>> df.assign(ln_A = lambda x: np.log(x.A))
3363-
A B ln_A
3364-
0 1 -1.0 0.000000
3365-
1 2 0.5 0.693147
3363+
>>> df.assign(state=lambda x: pd.Categorical(x.state),
3364+
temp_f=lambda x: x.temp_c * 9 / 5 + 32)
3365+
state temp_c
3366+
portland or 17.0
3367+
berkeley ca 25.0
33663368
33673369
Where the value already exists and is inserted:
33683370
3369-
>>> newcol = np.log(df['A'])
3370-
>>> df.assign(ln_A=newcol)
3371-
A B ln_A
3372-
0 1 -1.0 0.000000
3373-
1 2 0.5 0.693147
3371+
>>> newcol = df['temp_c'] * 9 / 5 + 32
3372+
>>> df.assign(temp_f=newcol)
3373+
state temp_c temp_f
3374+
portland or 17.0 62.6
3375+
berkeley ca 25.0 77.0
33743376
33753377
Where the keyword arguments depend on each other
33763378

0 commit comments

Comments
 (0)