Created
September 28, 2020 01:47
-
-
Save qiuyujx/b472f1663962381be31b98c3728949df to your computer and use it in GitHub Desktop.
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 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