Created
January 8, 2019 17:38
-
-
Save ashtonmeuser/f1639afa01b2bae80955ae282b20a40a to your computer and use it in GitHub Desktop.
Explode rows of a Pandas dataframe on character
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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