Stack the prescribed level(s) from columns to index. Return a reshaped DataFrame or Series having a multi-level index with one or more new inner-most levels compared to the current DataFrame. The new inner-most levels are created by pivoting the columns of t
(
self,
level: IndexLabel = -1,
dropna: bool | lib.NoDefault = lib.no_default,
sort: bool | lib.NoDefault = lib.no_default,
future_stack: bool = True,
)
| 11174 | ) |
| 11175 | |
| 11176 | def stack( |
| 11177 | self, |
| 11178 | level: IndexLabel = -1, |
| 11179 | dropna: bool | lib.NoDefault = lib.no_default, |
| 11180 | sort: bool | lib.NoDefault = lib.no_default, |
| 11181 | future_stack: bool = True, |
| 11182 | ): |
| 11183 | """ |
| 11184 | Stack the prescribed level(s) from columns to index. |
| 11185 | |
| 11186 | Return a reshaped DataFrame or Series having a multi-level |
| 11187 | index with one or more new inner-most levels compared to the current |
| 11188 | DataFrame. The new inner-most levels are created by pivoting the |
| 11189 | columns of the current dataframe: |
| 11190 | |
| 11191 | - if the columns have a single level, the output is a Series; |
| 11192 | - if the columns have multiple levels, the new index level(s) is (are) |
| 11193 | taken from the prescribed level(s) and the output is a DataFrame. |
| 11194 | |
| 11195 | Parameters |
| 11196 | ---------- |
| 11197 | level : int, str, list, default -1 |
| 11198 | Level(s) to stack from the column axis onto the index |
| 11199 | axis, defined as one index or label, or a list of indices |
| 11200 | or labels. |
| 11201 | dropna : bool, default True |
| 11202 | Whether to drop rows in the resulting Frame/Series with |
| 11203 | missing values. Stacking a column level onto the index |
| 11204 | axis can create combinations of index and column values |
| 11205 | that are missing from the original dataframe. See Examples |
| 11206 | section. |
| 11207 | sort : bool, default True |
| 11208 | Whether to sort the levels of the resulting MultiIndex. |
| 11209 | future_stack : bool, default True |
| 11210 | Whether to use the new implementation that will replace the current |
| 11211 | implementation in pandas 3.0. When True, dropna and sort have no impact |
| 11212 | on the result and must remain unspecified. See :ref:`pandas 2.1.0 Release |
| 11213 | notes <whatsnew_210.enhancements.new_stack>` for more details. |
| 11214 | |
| 11215 | Returns |
| 11216 | ------- |
| 11217 | DataFrame or Series |
| 11218 | Stacked dataframe or series. |
| 11219 | |
| 11220 | See Also |
| 11221 | -------- |
| 11222 | DataFrame.unstack : Unstack prescribed level(s) from index axis |
| 11223 | onto column axis. |
| 11224 | DataFrame.pivot : Reshape dataframe from long format to wide |
| 11225 | format. |
| 11226 | DataFrame.pivot_table : Create a spreadsheet-style pivot table |
| 11227 | as a DataFrame. |
| 11228 | |
| 11229 | Notes |
| 11230 | ----- |
| 11231 | The function is named by analogy with a collection of books |
| 11232 | being reorganized from being side by side on a horizontal |
| 11233 | position (the columns of the dataframe) to being stacked |