Last active
September 5, 2022 15:05
-
-
Save ivsanro1/1f286eac7ec67835c5e0b139a8adced9 to your computer and use it in GitHub Desktop.
json_normalize_and_concat
This file contains hidden or 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
| 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