Skip to content

Commit 58942fc

Browse files
datapythonistaaeltanawy
authored andcommitted
Working on the assign docstring
1 parent db97089 commit 58942fc

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
@@ -3250,38 +3250,24 @@ def assign(self, **kwargs):
32503250
32513251
Examples
32523252
--------
3253-
>>> df = pd.DataFrame({'A': range(1, 11), 'B': np.random.randn(10)})
3253+
>>> df = pd.DataFrame([('or', 17.),('ca', 25)],
3254+
index=['portland', 'berkeley'],
3255+
columns=['state', 'temp_c'])
32543256
32553257
Where the value is a callable, evaluated on `df`:
32563258
3257-
>>> df.assign(ln_A = lambda x: np.log(x.A))
3258-
A B ln_A
3259-
0 1 0.426905 0.000000
3260-
1 2 -0.780949 0.693147
3261-
2 3 -0.418711 1.098612
3262-
3 4 -0.269708 1.386294
3263-
4 5 -0.274002 1.609438
3264-
5 6 -0.500792 1.791759
3265-
6 7 1.649697 1.945910
3266-
7 8 -1.495604 2.079442
3267-
8 9 0.549296 2.197225
3268-
9 10 -0.758542 2.302585
3259+
>>> df.assign(temp_f=lambda x: x.temp_c * 9 / 5 + 32)
3260+
state temp_c temp_f
3261+
portland or 17.0 62.6
3262+
berkeley ca 25.0 77.0
32693263
32703264
Where the value already exists and is inserted:
32713265
3272-
>>> newcol = np.log(df['A'])
3273-
>>> df.assign(ln_A=newcol)
3274-
A B ln_A
3275-
0 1 0.426905 0.000000
3276-
1 2 -0.780949 0.693147
3277-
2 3 -0.418711 1.098612
3278-
3 4 -0.269708 1.386294
3279-
4 5 -0.274002 1.609438
3280-
5 6 -0.500792 1.791759
3281-
6 7 1.649697 1.945910
3282-
7 8 -1.495604 2.079442
3283-
8 9 0.549296 2.197225
3284-
9 10 -0.758542 2.302585
3266+
>>> newcol = df['temp_c'] * 9 / 5 + 32
3267+
>>> df.assign(temp_f=newcol)
3268+
state temp_c temp_f
3269+
portland or 17.0 62.6
3270+
berkeley ca 25.0 77.0
32853271
32863272
Where the keyword arguments depend on each other
32873273

0 commit comments

Comments
 (0)