Created
August 21, 2018 02:33
-
-
Save jerryan999/db89e72638858e0c84a3325a8cbb9fe7 to your computer and use it in GitHub Desktop.
split dataframe list into multiple rows
This file contains 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
def splitDataFrameList(df,target_column): | |
''' df = dataframe to split, | |
target_column = the column containing the values to split | |
returns: a dataframe with each entry for the target column separated, with each element moved into a new row. | |
The values in the other columns are duplicated across the newly divided rows. | |
''' | |
def splitListToRows(row,row_accumulator,target_column): | |
split_row = row[target_column] | |
for s in split_row: | |
new_row = row.to_dict() | |
new_row[target_column] = s | |
row_accumulator.append(new_row) | |
new_rows = [] | |
df.apply(splitListToRows,axis=1,args = (new_rows,target_column)) | |
new_df = pd.DataFrame(new_rows) | |
return new_df |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If the column has zero length list, no new rows will be generated