Skip to content

explode multiple columns at same time #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 14, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 22 additions & 9 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -6261,18 +6261,31 @@ def explode(self, column: Union[str, Tuple]) -> "DataFrame":
3 3 1
3 4 1
"""

if not (is_scalar(column) or isinstance(column, tuple)):
raise ValueError("column must be a scalar")
if not self.columns.is_unique:
raise ValueError("columns must be unique")

result = self[column].explode()
return (
self.drop([column], axis=1)
.join(result)
.reindex(columns=self.columns, copy=False)
)
if isinstance(columns, str):
columns = [columns]

if not isinstance(columns, list):
raise TypeError("columns value not list or sting")

if not all([c in self.columns for c in columns]):
raise ValueError("column name(s) not in index")

tmp = pd.DataFrame()
lengths_equal = []
for row in self[columns].iterrows():
r = row[1]
lengths_equal.append(len(set([len(r[c]) for c in columns]))==1)
if all(lengths_equal):
for c in columns:
tmp[c] = self[c].explode()
else:
ValueError("lengths of lists in the same row not equal")

results = self.drop(columns, axis=1).join(tmp)
return(results)

def unstack(self, level=-1, fill_value=None):
"""
Expand Down