@@ -5250,36 +5250,166 @@ def pivot_table(self, values=None, index=None, columns=None,
5250
5250
5251
5251
def stack (self , level = - 1 , dropna = True ):
5252
5252
"""
5253
- Pivot a level of the (possibly hierarchical) column labels, returning a
5254
- DataFrame (or Series in the case of an object with a single level of
5255
- column labels) having a hierarchical index with a new inner-most level
5256
- of row labels.
5257
- The level involved will automatically get sorted.
5253
+ Stack the prescribed level(s) from columns to index.
5254
+
5255
+ Return a reshaped DataFrame or Series having a multi-level
5256
+ index with one or more new inner-most levels compared to the current
5257
+ DataFrame. The new inner-most levels are created by pivoting the
5258
+ columns of the current dataframe:
5259
+
5260
+ - if the columns have a single level, the output is a Series;
5261
+ - if the columns have multiple levels, the new index
5262
+ level(s) is (are) taken from the prescribed level(s) and
5263
+ the output is a DataFrame.
5264
+
5265
+ The new index levels are sorted.
5258
5266
5259
5267
Parameters
5260
5268
----------
5261
- level : int, string, or list of these, default last level
5262
- Level(s) to stack, can pass level name
5263
- dropna : boolean, default True
5264
- Whether to drop rows in the resulting Frame/Series with no valid
5265
- values
5269
+ level : int, str, list, default -1
5270
+ Level(s) to stack from the column axis onto the index
5271
+ axis, defined as one index or label, or a list of indices
5272
+ or labels.
5273
+ dropna : bool, default True
5274
+ Whether to drop rows in the resulting Frame/Series with
5275
+ missing values. Stacking a column level onto the index
5276
+ axis can create combinations of index and column values
5277
+ that are missing from the original dataframe. See Examples
5278
+ section.
5279
+
5280
+ Returns
5281
+ -------
5282
+ DataFrame or Series
5283
+ Stacked dataframe or series.
5284
+
5285
+ See Also
5286
+ --------
5287
+ DataFrame.unstack : Unstack prescribed level(s) from index axis
5288
+ onto column axis.
5289
+ DataFrame.pivot : Reshape dataframe from long format to wide
5290
+ format.
5291
+ DataFrame.pivot_table : Create a spreadsheet-style pivot table
5292
+ as a DataFrame.
5293
+
5294
+ Notes
5295
+ -----
5296
+ The function is named by analogy with a collection of books
5297
+ being re-organised from being side by side on a horizontal
5298
+ position (the columns of the dataframe) to being stacked
5299
+ vertically on top of of each other (in the index of the
5300
+ dataframe).
5266
5301
5267
5302
Examples
5268
- ----------
5269
- >>> s
5270
- a b
5271
- one 1. 2.
5272
- two 3. 4.
5303
+ --------
5304
+ **Single level columns**
5305
+
5306
+ >>> df_single_level_cols = pd.DataFrame([[0, 1], [2, 3]],
5307
+ ... index=['cat', 'dog'],
5308
+ ... columns=['weight', 'height'])
5309
+
5310
+ Stacking a dataframe with a single level column axis returns a Series:
5311
+
5312
+ >>> df_single_level_cols
5313
+ weight height
5314
+ cat 0 1
5315
+ dog 2 3
5316
+ >>> df_single_level_cols.stack()
5317
+ cat weight 0
5318
+ height 1
5319
+ dog weight 2
5320
+ height 3
5321
+ dtype: int64
5273
5322
5274
- >>> s.stack()
5275
- one a 1
5276
- b 2
5277
- two a 3
5278
- b 4
5323
+ **Multi level columns: simple case**
5324
+
5325
+ >>> multicol1 = pd.MultiIndex.from_tuples([('weight', 'kg'),
5326
+ ... ('weight', 'pounds')])
5327
+ >>> df_multi_level_cols1 = pd.DataFrame([[1, 2], [2, 4]],
5328
+ ... index=['cat', 'dog'],
5329
+ ... columns=multicol1)
5330
+
5331
+ Stacking a dataframe with a multi-level column axis:
5332
+
5333
+ >>> df_multi_level_cols1
5334
+ weight
5335
+ kg pounds
5336
+ cat 1 2
5337
+ dog 2 4
5338
+ >>> df_multi_level_cols1.stack()
5339
+ weight
5340
+ cat kg 1
5341
+ pounds 2
5342
+ dog kg 2
5343
+ pounds 4
5344
+
5345
+ **Missing values**
5346
+
5347
+ >>> multicol2 = pd.MultiIndex.from_tuples([('weight', 'kg'),
5348
+ ... ('height', 'm')])
5349
+ >>> df_multi_level_cols2 = pd.DataFrame([[1.0, 2.0], [3.0, 4.0]],
5350
+ ... index=['cat', 'dog'],
5351
+ ... columns=multicol2)
5352
+
5353
+ It is common to have missing values when stacking a dataframe
5354
+ with multi-level columns, as the stacked dataframe typically
5355
+ has more values than the original dataframe. Missing values
5356
+ are filled with NaNs:
5357
+
5358
+ >>> df_multi_level_cols2
5359
+ weight height
5360
+ kg m
5361
+ cat 1.0 2.0
5362
+ dog 3.0 4.0
5363
+ >>> df_multi_level_cols2.stack()
5364
+ height weight
5365
+ cat kg NaN 1.0
5366
+ m 2.0 NaN
5367
+ dog kg NaN 3.0
5368
+ m 4.0 NaN
5369
+
5370
+ **Prescribing the level(s) to be stacked**
5371
+
5372
+ The first parameter controls which level or levels are stacked:
5373
+
5374
+ >>> df_multi_level_cols2.stack(0)
5375
+ kg m
5376
+ cat height NaN 2.0
5377
+ weight 1.0 NaN
5378
+ dog height NaN 4.0
5379
+ weight 3.0 NaN
5380
+ >>> df_multi_level_cols2.stack([0, 1])
5381
+ cat height m 2.0
5382
+ weight kg 1.0
5383
+ dog height m 4.0
5384
+ weight kg 3.0
5385
+ dtype: float64
5279
5386
5280
- Returns
5281
- -------
5282
- stacked : DataFrame or Series
5387
+ **Dropping missing values**
5388
+
5389
+ >>> df_multi_level_cols3 = pd.DataFrame([[None, 1.0], [2.0, 3.0]],
5390
+ ... index=['cat', 'dog'],
5391
+ ... columns=multicol2)
5392
+
5393
+ Note that rows where all values are missing are dropped by
5394
+ default but this behaviour can be controlled via the dropna
5395
+ keyword parameter:
5396
+
5397
+ >>> df_multi_level_cols3
5398
+ weight height
5399
+ kg m
5400
+ cat NaN 1.0
5401
+ dog 2.0 3.0
5402
+ >>> df_multi_level_cols3.stack(dropna=False)
5403
+ height weight
5404
+ cat kg NaN NaN
5405
+ m 1.0 NaN
5406
+ dog kg NaN 2.0
5407
+ m 3.0 NaN
5408
+ >>> df_multi_level_cols3.stack(dropna=True)
5409
+ height weight
5410
+ cat m 1.0 NaN
5411
+ dog kg NaN 2.0
5412
+ m 3.0 NaN
5283
5413
"""
5284
5414
from pandas .core .reshape .reshape import stack , stack_multiple
5285
5415
0 commit comments