From 6326745e18426bc6f07064b587e560834f9d6151 Mon Sep 17 00:00:00 2001 From: Kyle Stahl Date: Mon, 16 Sep 2019 11:25:35 -0500 Subject: [PATCH 1/2] ENH: DataFrame.explode() multiple columns Now explode() can also take in a list of columns and explode them all, given that for every record in the dataframe the elements of the exploding columns all have the same length --- pandas/core/frame.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index f7b6df8299abf..77ce723ebba59 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -6298,7 +6298,7 @@ def explode(self, columns: Union[str, List[str]]) -> "DataFrame": lengths_equal = [] for row in self[columns].iterrows(): - # converts non-lists into 1 element lists + # converts non-lists into 1 element lists so len() is valid r=row[1].apply(lambda x: x if type(x) in (list,tuple) else [x]) # make sure all lists in the same record are the same length From fa0df4223dd0a0fc8d716359dbd3451912912009 Mon Sep 17 00:00:00 2001 From: Kyle Stahl Date: Mon, 16 Sep 2019 11:31:43 -0500 Subject: [PATCH 2/2] ENH: explode multiple columns --- pandas/core/frame.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 77ce723ebba59..930fac1322bde 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -6207,14 +6207,15 @@ def stack(self, level=-1, dropna=True): def explode(self, columns: Union[str, List[str]]) -> "DataFrame": """ - Transform each element of a list-like to a row, replicating the index values. + Transform each element of a list-like to a row, replicating index values. .. versionadded:: 0.25.0 Parameters ---------- - column : str or tuple - + columns : str or list + the column(s) to be exploded + Returns ------- DataFrame