Skip to content

Commit ac80d60

Browse files
committed
add usecase
1 parent 5d8feb0 commit ac80d60

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

doc/source/user_guide/reshaping.rst

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -838,3 +838,19 @@ This routine will replace empty lists with ``np.nan`` and preserve scalar entrie
838838
s = pd.Series([[1, 2, 3], 'foo', [], ['a', 'b']])
839839
s
840840
s.explode()
841+
842+
Here is a typical usecase. You have comma separated string in a column.
843+
844+
.. ipython:: python
845+
846+
df = DataFrame([{'var1': 'a,b,c', 'var2': 1},
847+
{'var1': 'd,e,f', 'var2': 2}])
848+
df
849+
850+
Creating a long form DataFrame is now straightforward using chained operations
851+
852+
.. ipython:: python
853+
854+
exploded = df.var1.str.split(',').explode()
855+
exploded
856+
df[['var2']].join(exploded)

doc/source/whatsnew/v0.25.0.rst

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ The repr now looks like this:
187187
Series.explode to split list-like values to rows
188188
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
189189

190-
:class:`Series` has gained the :meth:`Series.explode` method to transform list-likes to individual rows. See :ref:`section on Exploding list-like column <reshaping.explode>` in docs for more information (:issue:`16538`)
190+
:class:`Series` has gained the :meth:`Series.explode` method to transform list-likes to individual rows. See :ref:`section on Exploding list-like column <reshaping.explode>` in docs for more information (:issue:`16538`, :issue:`10511`)
191191

192192

193193
.. ipython:: python
@@ -196,6 +196,22 @@ Series.explode to split list-like values to rows
196196
s
197197
s.explode()
198198
199+
Here is a typical usecase. You have comma separated string in a column.
200+
201+
.. ipython:: python
202+
203+
df = DataFrame([{'var1': 'a,b,c', 'var2': 1},
204+
{'var1': 'd,e,f', 'var2': 2}])
205+
df
206+
207+
Creating a long form DataFrame is now straightforward using chained operations
208+
209+
.. ipython:: python
210+
211+
exploded = df.var1.str.split(',').explode()
212+
exploded
213+
df[['var2']].join(exploded)
214+
199215
.. _whatsnew_0250.enhancements.other:
200216

201217
Other enhancements

pandas/tests/series/test_explode.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,15 @@ def test_invert_array():
6666
listify = df.apply(lambda x: x.array, axis=1)
6767
result = listify.explode()
6868
tm.assert_series_equal(result, df["a"].rename())
69+
70+
71+
def test_typical_usecase():
72+
73+
df = pd.DataFrame([{"var1": "a,b,c", "var2": 1}, {"var1": "d,e,f", "var2": 2}])
74+
exploded = df.var1.str.split(",").explode()
75+
exploded
76+
result = df[["var2"]].join(exploded)
77+
expected = pd.DataFrame(
78+
{"var2": [1, 1, 1, 2, 2, 2], "var1": list("abcdef")}, index=[0, 0, 0, 1, 1, 1]
79+
)
80+
tm.assert_frame_equal(result, expected, check_like=True)

0 commit comments

Comments
 (0)