Pandas Series: explode() function
Explode list-likes including lists, tuples, Series, and np.ndarray
The explode() function is used to transform each element of a list-like to a row, replicating the index values.
Syntax:
Series.explode(self) → 'Series'
Returns: Series- Exploded lists to rows; index will be duplicated for these rows.
Notes:
This routine will explode list-likes including lists, tuples, Series, and np.ndarray. The result dtype of the subset rows will be object. Scalars will be returned unchanged. Empty list-likes will result in a np.nan for that row.
Example:
Python-Pandas Code:
import numpy as np
import pandas as pd
s = pd.Series([[2, 3, 4], 'ff', [], [5, 6]])
s
Output:
0 [2, 3, 4] 1 ff 2 [] 3 [5, 6] dtype: object
Python-Pandas Code:
import numpy as np
import pandas as pd
s = pd.Series([[2, 3, 4], 'ff', [], [5, 6]])
s.explode()
Output:
0 2 0 3 0 4 1 ff 2 NaN 3 5 3 6 dtype: object
Previous: Series-unstack() function
Next: Find indices where elements should be inserted to maintain order
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/pandas/series/series-explode.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics