@@ -5178,10 +5178,17 @@ def stack(self, level=-1, dropna=True):
5178
5178
>>> df_single_level_cols = pd.DataFrame([[0, 1], [2, 3]],
5179
5179
... index=['one', 'two'],
5180
5180
... columns=['a', 'b'])
5181
- >>> multicol = pd.MultiIndex.from_tuples([('X', 'a'), ('X', 'b')])
5182
- >>> df_multi_level_cols = pd.DataFrame([[0, 1], [2, 3]],
5181
+ >>> multicol1 = pd.MultiIndex.from_tuples([('X', 'a'), ('X', 'b')])
5182
+ >>> df_multi_level_cols1 = pd.DataFrame([[0, 1], [2, 3]],
5183
5183
... index=['one', 'two'],
5184
- ... columns=multicol)
5184
+ ... columns=multicol1)
5185
+ >>> multicol2 = pd.MultiIndex.from_tuples([('X', 'a'), ('Y', 'b')])
5186
+ >>> df_multi_level_cols2 = pd.DataFrame([[0.0, 1.0], [2.0, 3.0]],
5187
+ ... index=['one', 'two'],
5188
+ ... columns=multicol2)
5189
+ >>> df_multi_level_cols3 = pd.DataFrame([[None, 1.0], [2.0, 3.0]],
5190
+ ... index=['one', 'two'],
5191
+ ... columns=multicol2)
5185
5192
5186
5193
Stacking a dataframe with a single level column axis returns a Series:
5187
5194
@@ -5198,31 +5205,27 @@ def stack(self, level=-1, dropna=True):
5198
5205
5199
5206
Stacking a dataframe with a multi-level column axis with no missing values:
5200
5207
5201
- >>> df_multi_level_cols
5208
+ >>> df_multi_level_cols1
5202
5209
X
5203
5210
a b
5204
5211
one 0 1
5205
5212
two 2 3
5206
- >>> df_multi_level_cols .stack()
5213
+ >>> df_multi_level_cols1 .stack()
5207
5214
X
5208
5215
one a 0
5209
5216
b 1
5210
5217
two a 2
5211
5218
b 3
5212
5219
5213
- Stacking a dataframe with a multi-level column axis with no missing values
5220
+ Stacking a dataframe with a multi-level column axis with no missing values.
5221
+ By default the missing values are filled with NaNs:
5214
5222
5215
- >>> multicol = pd.MultiIndex.from_tuples([('X', 'a'), ('Y', 'b')])
5216
- >>> s = pd.DataFrame([[0.0, 1.0], [2.0, 3.0]], index=['one', 'two'], columns=multicol)
5217
- >>> s
5223
+ >>> df_multi_level_cols2
5218
5224
X Y
5219
5225
a b
5220
5226
one 0.0 1.0
5221
5227
two 2.0 3.0
5222
-
5223
- By default the missing values are filled with NaNs:
5224
-
5225
- >>> s.stack()
5228
+ >>> df_multi_level_cols2.stack()
5226
5229
X Y
5227
5230
one a 0.0 NaN
5228
5231
b NaN 1.0
@@ -5231,22 +5234,20 @@ def stack(self, level=-1, dropna=True):
5231
5234
5232
5235
Rows where all values are missing are dropped by default:
5233
5236
5234
- >>> multicol = pd.MultiIndex.from_tuples([('X', 'a'), ('Y', 'b')])
5235
- >>> s = pd.DataFrame([[None, 1.0], [2.0, 3.0]], index=['one', 'two'], columns=multicol)
5236
- >>> s
5237
+ >>> df_multi_level_cols3
5237
5238
X Y
5238
5239
a b
5239
5240
one NaN 1.0
5240
5241
two 2.0 3.0
5241
5242
5242
- >>> s .stack(dropna=False)
5243
+ >>> df_multi_level_cols3 .stack(dropna=False)
5243
5244
X Y
5244
5245
one a NaN NaN
5245
5246
b NaN 1.0
5246
5247
two a 2.0 NaN
5247
5248
b NaN 3.0
5248
5249
5249
- >>> s .stack(dropna=True)
5250
+ >>> df_multi_level_cols3 .stack(dropna=True)
5250
5251
X Y
5251
5252
one b NaN 1.0
5252
5253
two a 2.0 NaN
0 commit comments