Skip to content

Instantly share code, notes, and snippets.

@aydinemre
Created February 24, 2021 15:30
Show Gist options
  • Save aydinemre/454da6d24e117a07c113bbe6603d5226 to your computer and use it in GitHub Desktop.
Save aydinemre/454da6d24e117a07c113bbe6603d5226 to your computer and use it in GitHub Desktop.
pandas json type columns converter
import pandas as pd
def json2df(json_df):
def normalize_column(dataframe, column):
df = pd.json_normalize(dataframe[column]) # Normalize column
df.columns = [f"{column}.{col}" for col in df.columns]
return pd.concat([dataframe, df], axis=1).drop(columns=column)
# id
json_df['_id'] = json_df['_id'].apply(lambda _id: _id.get('$oid'))
# city
json_df = normalize_column(json_df, 'city') # Normalize 'city' Column
# list
json_df = json_df.explode('list').reset_index(drop=True) # List type column to rows.
json_df = normalize_column(json_df, 'list') # Normalize 'list' Column
json_df = json_df.explode('list.weather').reset_index(drop=True) # List type column to rows.
json_df = normalize_column(json_df, 'list.weather') # Normalize 'list.weather' Column
return json_df
df = pd.read_json('/content/forecast_ist.json', orient='records')
processed_df = json2df(df)
display(processed_df.head())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment