Skip to content

Instantly share code, notes, and snippets.

@ivsanro1
Last active September 5, 2022 15:05
Show Gist options
  • Select an option

  • Save ivsanro1/1f286eac7ec67835c5e0b139a8adced9 to your computer and use it in GitHub Desktop.

Select an option

Save ivsanro1/1f286eac7ec67835c5e0b139a8adced9 to your computer and use it in GitHub Desktop.
json_normalize_and_concat
import pandas as pd
def json_normalize_and_concat(df: pd.DataFrame, col: str) -> pd.DataFrame:
index_name = df.reset_index().columns[0]
df_json_norm = pd.json_normalize(df[col], max_level=0).reset_index(drop=True)
df_ret = pd.concat([
df.reset_index().drop([c for c in df_json_norm.columns if c in df.columns], axis=1),
df_json_norm
], axis=1).set_index(index_name).drop(col, axis=1)
return df_ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment