Skip to content

Instantly share code, notes, and snippets.

@qiuyujx
Created September 28, 2020 01:47
Show Gist options
  • Save qiuyujx/b472f1663962381be31b98c3728949df to your computer and use it in GitHub Desktop.
Save qiuyujx/b472f1663962381be31b98c3728949df to your computer and use it in GitHub Desktop.
def expand_collection_type_column(df, target_col, col_names=None):
# Expand the list into multiple columns
df_expand = df[target_col].apply(pd.Series)
# Concatenate the expanded data frame with the original one
df = pd.concat([df, df_expand], axis=1).drop(['values'], axis=1)
# Rename columns if passed in
if col_names:
df = df.rename(columns={index:name for index, name in enumerate(col_names)})
return df
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment