import pandas as pd
import json
from pandas.io.json import json_normalize

json_columns = ['device', 'geoNetwork', 'totals', 'trafficSource']


def load_data_frame(path):
    df = pd.read_csv(path, converters={column: json.loads for column in json_columns}, dtype={'fullVisitorId': 'str'})
    # c = pd.read_csv("test.csv", converters={column: json.loads for column in json_columns}, dtype={'fullVisitorId': 'str'})
    for column in json_columns:
        column_as_df = json_normalize(df[column].tolist())
        column_as_df.columns = [f"{column}_{sub_column}" for sub_column in column_as_df.columns]
        df = df.drop(column, axis=1).merge(column_as_df, right_index=True, left_index=True)
    return df