Home > OS >  Pandas from json exploding with the custom prefix
Pandas from json exploding with the custom prefix

Time:01-21

On input i have pandas dataframe with nested columns/values. I do run

json_struct = json.loads(df.to_json(orient="records"))
df = pd.io.json.json_normalize(json_struct)

This way i explode/flatten columns. It's working fine.

The point is: i wanted to add new columns with non default prefix (currently flattened columns are with '.' character, while i want '_').

Example: instead of getting column level1.level2.level3 i want to get column level1_level2_level3.

I've tried to pass record_prefix or meta_prefix argument to pd.io.json.json_normalize but it does not work. Any hint how to do that ?

Thanks,

CodePudding user response:

Change sep parameter to the desired one:

sep : str, default ‘.’

Nested records will generate names separated by sep. e.g., for sep=’.’, {‘foo’: {‘bar’: 0}} -> foo.bar.
df = pd.json_normalize(json_struct, sep='_')
  • Related