Skip to content

Instantly share code, notes, and snippets.

@ashtonmeuser
Created January 8, 2019 17:38
Show Gist options
  • Select an option

  • Save ashtonmeuser/f1639afa01b2bae80955ae282b20a40a to your computer and use it in GitHub Desktop.

Select an option

Save ashtonmeuser/f1639afa01b2bae80955ae282b20a40a to your computer and use it in GitHub Desktop.
Explode rows of a Pandas dataframe on character
import pandas as pd
def explode_dataframe_column(df, target_column, separator=','):
"""
Explode target column on seperator
"""
row_accumulator = []
def splitListToRows(row, separator):
split_row = row[target_column].split(separator)
for s in split_row:
new_row = row.to_dict()
new_row[target_column] = s
row_accumulator.append(new_row)
df.apply(splitListToRows, axis=1, args=(separator, ))
new_df = pd.DataFrame(row_accumulator)
return new_df
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment