Created
November 29, 2018 09:05
-
-
Save Den1al/4d989a85ef56e7f3371f06454c64fe38 to your computer and use it in GitHub Desktop.
An implementation of HIVE's explode function in pandas
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 explode(df, by_column, new_col_name): | |
rows = [] | |
for row in df.itertuples(): | |
for value in getattr(row, by_column): | |
rows.append({ | |
new_col_name: value, | |
**{ | |
key: getattr(row, key) | |
for key in row._fields | |
if key not in ['Index', by_column] | |
} | |
}) | |
return pd.DataFrame(rows) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment