Quantcast
Channel: Data Science, Analytics and Big Data discussions - Latest topics
Viewing all articles
Browse latest Browse all 4448

HOW to read JSON file in Python

$
0
0

@rock_bt wrote:

Hey All,
I want to read JSON file, can someone tell what is wrong in the below code:

import numpy as np
import pandas as pd
import zipfile
import json
from pandas.io.json import json_normalize

JSON_COLUMNS = ['device', 'geoNetwork', 'totals', 'trafficSource']
df1 = pd.read_csv(zipfile.ZipFile('t2.zip').open('t2.csv'), 
                     converters={column: json.loads for column in JSON_COLUMNS},dtype={'fullVisitorId': 'str'})
for column in JSON_COLUMNS:
        column_as_df = json_normalize(df1[column])
        column_as_df.columns = [f"{column}.{subcolumn}" for subcolumn in column_as_df.columns]
        df1 = df1.drop(column, axis=1).merge(column_as_df, right_index=True, left_index=True)
df1.head()

Posts: 1

Participants: 1

Read full topic


Viewing all articles
Browse latest Browse all 4448

Trending Articles