Repository: pandas-dev/pandas
Pull request: ENH: add ignore_index option in DataFrame.explode
- [x] closes #34932 
- [x] tests added / passed
- [x] passes `black pandas`
- [x] passes `git diff upstream/master -u -- "*.py" | flake8 --diff`
- [x] whatsnew entry


Problem statement:
ENH: add ignore_index argument to DataFrame.explode / Series.explode
When we use `DataFrame.explode`  right now, it will repeat the index for each element in the iterator. To keep it consistent with the methods like `DataFrame.sort_values`, `DataFrame.append` and `pd.concat`, we can add an argument `ignore_index`, which will reset the index.


```python
df = pd.DataFrame({'id':range(0,30,10),
                   'values':[list('abc'), list('def'), list('ghi')]})
print(df)

   id     values
0   0  [a, b, c]
1  10  [d, e, f]
2  20  [g, h, i]

print(df.explode('values'))
   id values
0   0      a
0   0      b
0   0      c
1  10      d
1  10      e
1  10      f
2  20      g
2  20      h
2  20      i
```
Expected behaviour with addition of the argument:
```python
df.explode('values', ignore_index=True)

   id values
0   0      a
1   0      b
2   0      c
3  10      d
4  10      e
5  10      f
6  20      g
7  20      h
8  20      i
```

Hints from issue discussion:

Commit message: [IMP] - #34932 - added option to ignore_index in DataFrame.explod

Patch to review:
diff --git a/pandas/core/frame.py b/pandas/core/frame.py
index b522920ec9f23..4ff0def44873f 100644
--- a/pandas/core/frame.py
+++ b/pandas/core/frame.py
@@ -6896,7 +6896,7 @@ def stack(self, level=-1, dropna=True):
         else:
             return stack(self, level, dropna=dropna)
 
-    def explode(self, column: Union[str, Tuple]) -> "DataFrame":
+    def explode(self, column: Union[str, Tuple], ignore_index=False) -> "DataFrame":
         """
         Transform each element of a list-like to a row, replicating index values.
 
@@ -6961,8 +6961,11 @@ def explode(self, column: Union[str, Tuple]) -> "DataFrame":
         # TODO: use overload to refine return type of reset_index
         assert df is not None  # needed for mypy
         result = df[column].explode()
-        result = df.drop([column], axis=1).join(result)
-        result.index = self.index.take(result.index)
+        if ignore_index:
+            result = df.drop([column], axis=1).join(result).reset_index(drop=True)
+        else:
+            result = df.drop([column], axis=1).join(result)
+            result.index = self.index.take(result.index)
         result = result.reindex(columns=self.columns, copy=False)
 
         return result


Code review comments:
diff_hunk: @@ -6896,7 +6896,7 @@ def stack(self, level=-1, dropna=True):
         else:
             return stack(self, level, dropna=dropna)
 
-    def explode(self, column: Union[str, Tuple]) -> "DataFrame":
+    def explode(self, column: Union[str, Tuple], ignore_index=False) -> "DataFrame":
path: pandas/core/frame.py
text: @user1:
include the type for ignore_index.

@author:
Got it, done.