@@ -5173,6 +5173,12 @@ def stack(self, level=-1, dropna=True):
5173
5173
that are missing from the original dataframe. See Examples
5174
5174
section.
5175
5175
5176
+ Notes
5177
+ -----
5178
+ The function is named by analogy with a stack of books
5179
+ (levels) being re-organised from a horizontal position (column
5180
+ levels) to a vertical position (index levels).
5181
+
5176
5182
Examples
5177
5183
--------
5178
5184
>>> df_single_level_cols = pd.DataFrame([[0, 1], [2, 3]],
@@ -5203,7 +5209,7 @@ def stack(self, level=-1, dropna=True):
5203
5209
b 3
5204
5210
dtype: int64
5205
5211
5206
- Stacking a dataframe with a multi-level column axis with no missing values :
5212
+ Stacking a dataframe with a multi-level column axis:
5207
5213
5208
5214
>>> df_multi_level_cols1
5209
5215
X
@@ -5218,8 +5224,8 @@ def stack(self, level=-1, dropna=True):
5218
5224
b 3
5219
5225
5220
5226
It is common to have missing values when stacking a dataframe
5221
- with multi-level columns, since the stacked dataframe can have
5222
- more values than the original dataframe. By default the
5227
+ with multi-level columns, as the stacked dataframe typically
5228
+ has more values than the original dataframe. By default the
5223
5229
missing values are filled with NaNs:
5224
5230
5225
5231
>>> df_multi_level_cols2
@@ -5234,9 +5240,18 @@ def stack(self, level=-1, dropna=True):
5234
5240
two a 2.0 NaN
5235
5241
b NaN 3.0
5236
5242
5237
- Rows where all values are missing are dropped by default but
5238
- this behaviour can be controlled via the dropna keyword
5239
- parameter:
5243
+ Multiple levels can be stacked at once
5244
+
5245
+ >>> df_multi_level_cols2.stack([1, 0])
5246
+ one a X 0.0
5247
+ b Y 1.0
5248
+ two a X 2.0
5249
+ b Y 3.0
5250
+ dtype: float64
5251
+
5252
+ Note that rows where all values are missing are dropped by
5253
+ default but this behaviour can be controlled via the dropna
5254
+ keyword parameter:
5240
5255
5241
5256
>>> df_multi_level_cols3
5242
5257
X Y
0 commit comments