|
| 1 | +--- |
| 2 | +Title: '.explode()' |
| 3 | +Description: 'Transforms each element of a list-like column into a separate row.' |
| 4 | +Subjects: |
| 5 | + - 'Computer science' |
| 6 | + - 'Data Science' |
| 7 | +Tags: |
| 8 | + - 'DataFrames' |
| 9 | + - 'Functions' |
| 10 | + - 'Lists' |
| 11 | + - 'Pandas' |
| 12 | +CatalogContent: |
| 13 | + - 'learn-python-3' |
| 14 | + - 'paths/computer-science' |
| 15 | +--- |
| 16 | + |
| 17 | +In Pandas, the **`.explode()`** method transforms each element of a list-like column (such as lists, tuples, or arrays) into separate rows, while replicating the corresponding index values. This is especially helpful when working with columns that contain nested or iterable data that needs to be flattened for further analysis. |
| 18 | + |
| 19 | +## Syntax |
| 20 | + |
| 21 | +```pseudo |
| 22 | +DataFrame.explode(column, ignore_index=False) |
| 23 | +``` |
| 24 | + |
| 25 | +**Parameters:** |
| 26 | + |
| 27 | +- `column` (string or tuple): Specifies the name of the column to explode. The column must contain list-like elements such as lists, tuples, or arrays. |
| 28 | +- `ignore_index` (Optional): If set to `True`, the resulting DataFrame will have a new integer index ranging from `0` to `n - 1`. If `False`, the original index labels are retained and repeated as necessary. |
| 29 | + |
| 30 | +**Return value:** |
| 31 | + |
| 32 | +The `.explode()` method returns a new DataFrame in which each element of the specified list-like column is expanded into a separate row. Values in other columns are duplicated accordingly to align with the exploded rows. |
| 33 | + |
| 34 | +## Example |
| 35 | + |
| 36 | +The example below shows how to expand a column containing lists into multiple rows: |
| 37 | + |
| 38 | +```py |
| 39 | +import pandas as pd |
| 40 | + |
| 41 | +df = pd.DataFrame({ |
| 42 | + 'Name': ['Alice', 'Bob'], |
| 43 | + 'Hobbies': [['Reading', 'Cycling'], ['Painting']] |
| 44 | +}) |
| 45 | + |
| 46 | +exploded_df = df.explode('Hobbies') |
| 47 | + |
| 48 | +print(exploded_df) |
| 49 | +``` |
| 50 | + |
| 51 | +A possible output of this code is: |
| 52 | + |
| 53 | +```shell |
| 54 | + Name Hobbies |
| 55 | +0 Alice Reading |
| 56 | +0 Alice Cycling |
| 57 | +1 Bob Painting |
| 58 | +``` |
| 59 | + |
| 60 | +The code takes the `Hobbies` column, which contains lists, and creates one row for each item in the list, while preserving the associated values in the `Name` column. |
| 61 | + |
| 62 | +## Codebyte Example |
| 63 | + |
| 64 | +In this codebyte example, a column is exploded with tuple values: |
| 65 | + |
| 66 | +```codebyte/python |
| 67 | +import pandas as pd |
| 68 | +
|
| 69 | +df = pd.DataFrame({ |
| 70 | + 'ID': [1, 2], |
| 71 | + 'Scores': [(90, 80), (75, 85)] |
| 72 | +}) |
| 73 | +
|
| 74 | +exploded_df = df.explode('Scores') |
| 75 | +
|
| 76 | +print(exploded_df) |
| 77 | +``` |
| 78 | + |
| 79 | +Tuples are also treated as list-like by `.explode()`. |
| 80 | + |
| 81 | +> **Notes:** |
| 82 | +> |
| 83 | +> - `.explode()` is available in Pandas version 0.25.0 and later. |
| 84 | +> - Cells with non-list-like values (e.g., strings, numbers) are not exploded and appear as-is in the resulting DataFrame. |
0 commit comments