Skip to content

Instantly share code, notes, and snippets.

@Den1al
Created November 29, 2018 09:05
Show Gist options
  • Save Den1al/4d989a85ef56e7f3371f06454c64fe38 to your computer and use it in GitHub Desktop.
Save Den1al/4d989a85ef56e7f3371f06454c64fe38 to your computer and use it in GitHub Desktop.
An implementation of HIVE's explode function in pandas
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