From d2c7707c9ee2b7c9f6e04c83781dcd20dbe7ee02 Mon Sep 17 00:00:00 2001 From: Ian Pires de Campos Date: Fri, 16 May 2025 22:24:24 -0300 Subject: [PATCH 1/3] Added explode.md file --- .../dataframe/terms/explode/explode.md | 81 +++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 content/pandas/concepts/dataframe/terms/explode/explode.md diff --git a/content/pandas/concepts/dataframe/terms/explode/explode.md b/content/pandas/concepts/dataframe/terms/explode/explode.md new file mode 100644 index 00000000000..7ad7e4087b0 --- /dev/null +++ b/content/pandas/concepts/dataframe/terms/explode/explode.md @@ -0,0 +1,81 @@ +--- +Title: '.explode()' +Description: 'Transforms each element of a list-like column into a separate row.' +Subjects: + - 'Computer science' + - 'Data Science' +Tags: + - 'DataFrames' + - 'Lists' + - 'Functions' + - 'Pandas' + - 'Data Cleaning' +CatalogContent: + - 'learn-python-3' + - 'paths/computer-science' +--- + +In the pandas module, the **`.explode()`** method is used to transform each element of a list-like column (such as lists, tuples, or Series) into a separate row, replicating the index values for each item. This is especially useful when dealing with columns that contain nested or iterable data. + +## Syntax + +```pseudo +DataFrame.explode(column, ignore_index=False) +``` + +**Parameters:** + +- `column`: str or tuple. The name of the column to explode. The column must contain list-like elements. +- `ignore_index` (Optional): If `True`, the resulting index will be labeled from `0` to `n - 1`. The default is `False`, meaning the original index is preserved. + +**Return value:** + +The `.explode()` method returns a new DataFrame where each element of the specified list-like column becomes a separate row. Other columns are duplicated to match the new rows. + +## Example + +The example below shows how to expand a column containing lists into multiple rows: + +```py +import pandas as pd + +df = pd.DataFrame({ + 'Name': ['Alice', 'Bob'], + 'Hobbies': [['Reading', 'Cycling'], ['Painting']] +}) + +exploded_df = df.explode('Hobbies') + +print(exploded_df) +``` + +A possible output of this code is: + +```shell + Name Hobbies +0 Alice Reading +0 Alice Cycling +1 Bob Painting +``` + +The code above 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. + +## Codebyte Example + +In this codebyte example, we explode a column with tuple values: + +```codebyte/python +import pandas as pd + +df = pd.DataFrame({ + 'ID': [1, 2], + 'Scores': [(90, 80), (75, 85)] +}) + +exploded_df = df.explode('Scores', ignore_index=True) + +print(exploded_df) +``` + +> **Note:** The `.explode()` method is only available in pandas version 0.25.0 and above. +> **Note:** If a cell in the specified column contains a scalar (non-list) value, it will not be exploded and will remain as is. From 722636ba356e207aeaa65da05482af7d224b57c0 Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Sun, 18 May 2025 15:16:56 +0530 Subject: [PATCH 2/3] minor fixes --- .../dataframe/terms/explode/explode.md | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/content/pandas/concepts/dataframe/terms/explode/explode.md b/content/pandas/concepts/dataframe/terms/explode/explode.md index 7ad7e4087b0..991b1d79824 100644 --- a/content/pandas/concepts/dataframe/terms/explode/explode.md +++ b/content/pandas/concepts/dataframe/terms/explode/explode.md @@ -6,16 +6,15 @@ Subjects: - 'Data Science' Tags: - 'DataFrames' - - 'Lists' - 'Functions' + - 'Lists' - 'Pandas' - - 'Data Cleaning' CatalogContent: - 'learn-python-3' - 'paths/computer-science' --- -In the pandas module, the **`.explode()`** method is used to transform each element of a list-like column (such as lists, tuples, or Series) into a separate row, replicating the index values for each item. This is especially useful when dealing with columns that contain nested or iterable data. +In the pandas module, 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. ## Syntax @@ -25,12 +24,12 @@ DataFrame.explode(column, ignore_index=False) **Parameters:** -- `column`: str or tuple. The name of the column to explode. The column must contain list-like elements. -- `ignore_index` (Optional): If `True`, the resulting index will be labeled from `0` to `n - 1`. The default is `False`, meaning the original index is preserved. +- `column` (str or tuple): Specifies the name of the column to explode. The column must contain list-like elements such as lists, tuples, or arrays. +- `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. **Return value:** -The `.explode()` method returns a new DataFrame where each element of the specified list-like column becomes a separate row. Other columns are duplicated to match the new rows. +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. ## Example @@ -72,10 +71,13 @@ df = pd.DataFrame({ 'Scores': [(90, 80), (75, 85)] }) -exploded_df = df.explode('Scores', ignore_index=True) +exploded_df = df.explode('Scores') print(exploded_df) ``` -> **Note:** The `.explode()` method is only available in pandas version 0.25.0 and above. -> **Note:** If a cell in the specified column contains a scalar (non-list) value, it will not be exploded and will remain as is. +Tuples are also treated as list-like by `.explode()`. + +> **Note:** `.explode()` is available in pandas version 0.25.0 and later. + +> **Note:** Cells with non-list-like values (e.g., strings, numbers) are not exploded and appear as-is in the resulting DataFrame. From 4ec1bf532ae818b4a654d408bf9c09b8b72ba9e8 Mon Sep 17 00:00:00 2001 From: Sriparno Roy Date: Fri, 30 May 2025 19:47:03 +0530 Subject: [PATCH 3/3] Minor changes --- .../dataframe/terms/explode/explode.md | 23 ++++++++++--------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/content/pandas/concepts/dataframe/terms/explode/explode.md b/content/pandas/concepts/dataframe/terms/explode/explode.md index 991b1d79824..453404131a2 100644 --- a/content/pandas/concepts/dataframe/terms/explode/explode.md +++ b/content/pandas/concepts/dataframe/terms/explode/explode.md @@ -14,7 +14,7 @@ CatalogContent: - 'paths/computer-science' --- -In the pandas module, 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. +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. ## Syntax @@ -24,7 +24,7 @@ DataFrame.explode(column, ignore_index=False) **Parameters:** -- `column` (str or tuple): Specifies the name of the column to explode. The column must contain list-like elements such as lists, tuples, or arrays. +- `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. - `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. **Return value:** @@ -39,8 +39,8 @@ The example below shows how to expand a column containing lists into multiple ro import pandas as pd df = pd.DataFrame({ - 'Name': ['Alice', 'Bob'], - 'Hobbies': [['Reading', 'Cycling'], ['Painting']] + 'Name': ['Alice', 'Bob'], + 'Hobbies': [['Reading', 'Cycling'], ['Painting']] }) exploded_df = df.explode('Hobbies') @@ -57,18 +57,18 @@ A possible output of this code is: 1 Bob Painting ``` -The code above 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. +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. ## Codebyte Example -In this codebyte example, we explode a column with tuple values: +In this codebyte example, a column is exploded with tuple values: ```codebyte/python import pandas as pd df = pd.DataFrame({ - 'ID': [1, 2], - 'Scores': [(90, 80), (75, 85)] + 'ID': [1, 2], + 'Scores': [(90, 80), (75, 85)] }) exploded_df = df.explode('Scores') @@ -78,6 +78,7 @@ print(exploded_df) Tuples are also treated as list-like by `.explode()`. -> **Note:** `.explode()` is available in pandas version 0.25.0 and later. - -> **Note:** Cells with non-list-like values (e.g., strings, numbers) are not exploded and appear as-is in the resulting DataFrame. +> **Notes:** +> +> - `.explode()` is available in Pandas version 0.25.0 and later. +> - Cells with non-list-like values (e.g., strings, numbers) are not exploded and appear as-is in the resulting DataFrame.