Created
December 6, 2019 09:21
-
-
Save BurakaKrishna/538cdad998247b95f9b2898015360a8e to your computer and use it in GitHub Desktop.
pandas explode method for versions < 0.25.0
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
values = df[column].tolist() | |
n = len(df[column].tolist()) | |
counts = np.zeros(n, dtype='int64') | |
for i in range(n): | |
v = values[i] | |
if len(v): | |
counts[i] += len(v) | |
else: | |
# empty list-like, use a nan marker | |
counts[i] += 1 | |
result = np.empty(counts.sum(), dtype='object') | |
count = 0 | |
for i in range(n): | |
v = values[i] | |
if len(v): | |
for j in range(len(v)): | |
result[count] = v[j] | |
count += 1 | |
else: | |
# empty list-like, use a nan marker | |
result[count] = np.nan | |
count += 1 | |
from pandas.core.series import Series | |
rst = Series(result, index=df[column].index.repeat(counts),name = column_name) | |
df.drop([column_name], axis=1).join(rst).reindex(columns=df.columns, copy=False) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment