Transform each element of a list-like to a row, replicating index values. Parameters ---------- column : IndexLabel Column(s) to explode. For multiple columns, specify a non-empty list with each element be str or tuple, and all sp
(
self,
column: IndexLabel,
ignore_index: bool = False,
)
| 11380 | return result.__finalize__(self, method="stack") |
| 11381 | |
| 11382 | def explode( |
| 11383 | self, |
| 11384 | column: IndexLabel, |
| 11385 | ignore_index: bool = False, |
| 11386 | ) -> DataFrame: |
| 11387 | """ |
| 11388 | Transform each element of a list-like to a row, replicating index values. |
| 11389 | |
| 11390 | Parameters |
| 11391 | ---------- |
| 11392 | column : IndexLabel |
| 11393 | Column(s) to explode. |
| 11394 | For multiple columns, specify a non-empty list with each element |
| 11395 | be str or tuple, and all specified columns their list-like data |
| 11396 | on same row of the frame must have matching length. |
| 11397 | |
| 11398 | ignore_index : bool, default False |
| 11399 | If True, the resulting index will be labeled 0, 1, …, n - 1. |
| 11400 | |
| 11401 | Returns |
| 11402 | ------- |
| 11403 | DataFrame |
| 11404 | Exploded lists to rows of the subset columns; |
| 11405 | index will be duplicated for these rows. |
| 11406 | |
| 11407 | Raises |
| 11408 | ------ |
| 11409 | ValueError : |
| 11410 | * If columns of the frame are not unique. |
| 11411 | * If specified columns to explode is empty list. |
| 11412 | * If specified columns to explode have not matching count of |
| 11413 | elements rowwise in the frame. |
| 11414 | |
| 11415 | See Also |
| 11416 | -------- |
| 11417 | DataFrame.unstack : Pivot a level of the (necessarily hierarchical) |
| 11418 | index labels. |
| 11419 | DataFrame.melt : Unpivot a DataFrame from wide format to long format. |
| 11420 | Series.explode : Explode a DataFrame from list-like columns to long format. |
| 11421 | |
| 11422 | Notes |
| 11423 | ----- |
| 11424 | This routine will explode list-likes including lists, tuples, sets, |
| 11425 | Series, and np.ndarray. The result dtype of the subset rows will |
| 11426 | be object. Scalars will be returned unchanged, and empty list-likes will |
| 11427 | result in a np.nan for that row. In addition, the ordering of rows in the |
| 11428 | output will be non-deterministic when exploding sets. |
| 11429 | |
| 11430 | Reference :ref:`the user guide <reshaping.explode>` for more examples. |
| 11431 | |
| 11432 | Examples |
| 11433 | -------- |
| 11434 | >>> df = pd.DataFrame( |
| 11435 | ... { |
| 11436 | ... "A": [[0, 1, 2], "foo", [], [3, 4]], |
| 11437 | ... "B": 1, |
| 11438 | ... "C": [["a", "b", "c"], np.nan, [], ["d", "e"]], |
| 11439 | ... } |