def explode(self, df, explode_column, delimiter=","):
assert explode_column[-1] is "s"
df["id"] = df.index
wdf = pd.DataFrame(df[explode_column].str.split(delimiter).tolist()).stack().reset_index()
exploded_column = explode_column[:-1]
wdf.columns = ["id", "depth", exploded_column] ## plural form to singular form
wdf[exploded_column] = wdf[exploded_column].apply(lambda x: x.strip()) # trim
wdf.drop("depth", axis=1, inplace=True)
return pd.merge(df, wdf, on='id').drop(columns=["id", explode_column])
More than 5 years have passed since last update.
Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme