Skip to content

Instantly share code, notes, and snippets.

@BurakaKrishna
Created December 6, 2019 09:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BurakaKrishna/538cdad998247b95f9b2898015360a8e to your computer and use it in GitHub Desktop.
Save BurakaKrishna/538cdad998247b95f9b2898015360a8e to your computer and use it in GitHub Desktop.
pandas explode method for versions < 0.25.0
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