Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.