You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We can 'explode' this transforming each element of a list-like to a row, by using ``~Series.explode``. This will replicate the index values:
821
+
We can 'explode' this transforming each element of a list-like to a row, by using :meth:`~Series.explode`. This will replicate the index values:
822
822
823
823
.. ipython:: python
824
824
825
825
df['values'].explode()
826
826
827
-
You can join this with the original to get an expanded ``DataFrame``.
827
+
You can easily join this with the original to get an expanded ``DataFrame``.
828
828
829
829
.. ipython:: python
830
830
831
831
df[['keys']].join(df['values'].explode())
832
832
833
-
834
-
This routine will replace empty lists with ``np.nan`` and preserve scalar entries. The dtype of the resulting ``Series`` is always ``object``.
833
+
:meth:`Series.explode` will replace empty lists with ``np.nan`` and preserve scalar entries. The dtype of the resulting ``Series`` is always ``object``.
835
834
836
835
.. ipython:: python
837
836
838
837
s = pd.Series([[1, 2, 3], 'foo', [], ['a', 'b']])
839
838
s
840
839
s.explode()
841
840
842
-
Here is a typical usecase. You have comma separated string in a column.
841
+
Here is a typical usecase. You have comma separated strings in a column and want to expand this.
843
842
844
843
.. ipython:: python
845
844
846
-
df = DataFrame([{'var1': 'a,b,c', 'var2': 1},
847
-
{'var1': 'd,e,f', 'var2': 2}])
845
+
df =pd.DataFrame([{'var1': 'a,b,c', 'var2': 1},
846
+
{'var1': 'd,e,f', 'var2': 2}])
848
847
df
849
848
850
-
Creating a long form DataFrame is now straightforward using chained operations
849
+
Creating a long form DataFrame is now straightforward using explode and chained operations
0 commit comments