Skip to content

Instantly share code, notes, and snippets.

@zouweilin
zouweilin / separator.py
Last active April 22, 2019 12:28 — forked from jlln/separator.py
Efficiently split Pandas Dataframe cells containing lists into multiple rows, duplicating the other column's values.
def split_dataframe_rows(df,column_selectors, row_delimiter):
# we need to keep track of the ordering of the columns
def _split_list_to_rows(row,row_accumulator,column_selector,row_delimiter):
split_rows = {}
max_split = 0
for column_selector in column_selectors:
split_row = row[column_selector].split(row_delimiter)
split_rows[column_selector] = split_row
if len(split_row) > max_split:
max_split = len(split_row)