Transform each element of a list-like to a row. Parameters ---------- ignore_index : bool, default False If True, the resulting index will be labeled 0, 1, …, n - 1. Returns ------- Series Exploded lists to rows; inde
(self, ignore_index: bool = False)
| 4375 | return result |
| 4376 | |
| 4377 | def explode(self, ignore_index: bool = False) -> Series: |
| 4378 | """ |
| 4379 | Transform each element of a list-like to a row. |
| 4380 | |
| 4381 | Parameters |
| 4382 | ---------- |
| 4383 | ignore_index : bool, default False |
| 4384 | If True, the resulting index will be labeled 0, 1, …, n - 1. |
| 4385 | |
| 4386 | Returns |
| 4387 | ------- |
| 4388 | Series |
| 4389 | Exploded lists to rows; index will be duplicated for these rows. |
| 4390 | |
| 4391 | See Also |
| 4392 | -------- |
| 4393 | Series.str.split : Split string values on specified separator. |
| 4394 | Series.unstack : Unstack, a.k.a. pivot, Series with MultiIndex |
| 4395 | to produce DataFrame. |
| 4396 | DataFrame.melt : Unpivot a DataFrame from wide format to long format. |
| 4397 | DataFrame.explode : Explode a DataFrame from list-like |
| 4398 | columns to long format. |
| 4399 | |
| 4400 | Notes |
| 4401 | ----- |
| 4402 | This routine will explode list-likes including lists, tuples, sets, |
| 4403 | Series, and np.ndarray. The result dtype of the subset rows will |
| 4404 | be object. Scalars will be returned unchanged, and empty list-likes will |
| 4405 | result in an np.nan for that row. In addition, the ordering of elements in |
| 4406 | the output will be non-deterministic when exploding sets. |
| 4407 | |
| 4408 | Reference :ref:`the user guide <reshaping.explode>` for more examples. |
| 4409 | |
| 4410 | Examples |
| 4411 | -------- |
| 4412 | >>> s = pd.Series([[1, 2, 3], "foo", [], [3, 4]]) |
| 4413 | >>> s |
| 4414 | 0 [1, 2, 3] |
| 4415 | 1 foo |
| 4416 | 2 [] |
| 4417 | 3 [3, 4] |
| 4418 | dtype: object |
| 4419 | |
| 4420 | >>> s.explode() |
| 4421 | 0 1 |
| 4422 | 0 2 |
| 4423 | 0 3 |
| 4424 | 1 foo |
| 4425 | 2 NaN |
| 4426 | 3 3 |
| 4427 | 3 4 |
| 4428 | dtype: object |
| 4429 | """ |
| 4430 | if isinstance(self.dtype, ExtensionDtype): |
| 4431 | values, counts = self._values._explode() |
| 4432 | elif len(self) and is_object_dtype(self.dtype): |
| 4433 | values, counts = reshape.explode(np.asarray(self._values)) |
| 4434 | else: |