Skip to content

Commit 4452014

Browse files
committed
DOC: simplified examples in DataFrame.assign docstring
1 parent 4082b1d commit 4452014

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

pandas/core/frame.py

+12-11
Original file line numberDiff line numberDiff line change
@@ -3354,23 +3354,24 @@ 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(temp_f=lambda x: x.temp_c * 9 / 5 + 32)
3364+
state temp_c temp_f
3365+
portland or 17.0 62.6
3366+
berkeley ca 25.0 77.0
33663367
33673368
Where the value already exists and is inserted:
33683369
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
3370+
>>> newcol = df['temp_c'] * 9 / 5 + 32
3371+
>>> df.assign(temp_f=newcol)
3372+
state temp_c temp_f
3373+
portland or 17.0 62.6
3374+
berkeley ca 25.0 77.0
33743375
33753376
Where the keyword arguments depend on each other
33763377

0 commit comments

Comments
 (0)