Skip to content

Commit 3d44884

Browse files
Working on the assign docstring
1 parent 0828c25 commit 3d44884

File tree

1 file changed

+12
-26
lines changed

1 file changed

+12
-26
lines changed

pandas/core/frame.py

+12-26
Original file line numberDiff line numberDiff line change
@@ -3354,38 +3354,24 @@ def assign(self, **kwargs):
33543354
33553355
Examples
33563356
--------
3357-
>>> df = pd.DataFrame({'A': range(1, 11), 'B': np.random.randn(10)})
3357+
>>> df = pd.DataFrame([('or', 17.),('ca', 25)],
3358+
index=['portland', 'berkeley'],
3359+
columns=['state', 'temp_c'])
33583360
33593361
Where the value is a callable, evaluated on `df`:
33603362
3361-
>>> df.assign(ln_A = lambda x: np.log(x.A))
3362-
A B ln_A
3363-
0 1 0.426905 0.000000
3364-
1 2 -0.780949 0.693147
3365-
2 3 -0.418711 1.098612
3366-
3 4 -0.269708 1.386294
3367-
4 5 -0.274002 1.609438
3368-
5 6 -0.500792 1.791759
3369-
6 7 1.649697 1.945910
3370-
7 8 -1.495604 2.079442
3371-
8 9 0.549296 2.197225
3372-
9 10 -0.758542 2.302585
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
33733367
33743368
Where the value already exists and is inserted:
33753369
3376-
>>> newcol = np.log(df['A'])
3377-
>>> df.assign(ln_A=newcol)
3378-
A B ln_A
3379-
0 1 0.426905 0.000000
3380-
1 2 -0.780949 0.693147
3381-
2 3 -0.418711 1.098612
3382-
3 4 -0.269708 1.386294
3383-
4 5 -0.274002 1.609438
3384-
5 6 -0.500792 1.791759
3385-
6 7 1.649697 1.945910
3386-
7 8 -1.495604 2.079442
3387-
8 9 0.549296 2.197225
3388-
9 10 -0.758542 2.302585
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
33893375
33903376
Where the keyword arguments depend on each other
33913377

0 commit comments

Comments
 (0)